Android 使用SharedReferences保存应用程序';布景
比如说,我的应用程序有两个活动Android 使用SharedReferences保存应用程序';布景,android,sharedpreferences,Android,Sharedpreferences,比如说,我的应用程序有两个活动 活动1,带有按钮的主活动,可打开活动2 活动2,这是我的设置活动,有一个复选框 现在,如果复选框被选中或未选中,我将使用SharedReferences保存的值 现在我想在其他活动中使用此设置值,因此我在活动1中制作了一个toast,显示复选框是否选中 活动1中的代码:- if(new Activity2().getCheckStatus()) Toast.makeText(getApplicationContext(), "chcked", Toast.
if(new Activity2().getCheckStatus())
Toast.makeText(getApplicationContext(), "chcked", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "not chcked", Toast.LENGTH_SHORT).show();
public class Activity2 extends Activity implements OnClickListener
{
CheckBox check =null;
SharedPreferences settings;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getActionBar().setTitle("SETTING");
setContentView(R.layout.setting);
check =(CheckBox)findViewById(R.id.checkBox);
check.setOnClickListener(this);
settings = Activty2.this.getSharedPreferences("checkBox",0);
Boolean a = settings.getBoolean("isChecked", false);
check.setChecked(a);
}
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.checkBox:
SharedPreferences.Editor editor = settings.edit();
if(!check.isChecked())
{
check.setChecked(true);
editor.putBoolean("isChecked", true);
editor.commit();
}
else
{
check.setChecked(false);
editor.putBoolean("isChecked", false);
editor.commit();
}
break;
}
}
public boolean getCheckStatus()
{
SharedPreferences mysettings;
try{
mysettings = Activity2.this.getSharedPreferences("checkBox",0);
}catch(NullPointerException e)
{
return false;
}
return mysettings.getBoolean("isChecked", false);
}
}
活动2:-
if(new Activity2().getCheckStatus())
Toast.makeText(getApplicationContext(), "chcked", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "not chcked", Toast.LENGTH_SHORT).show();
public class Activity2 extends Activity implements OnClickListener
{
CheckBox check =null;
SharedPreferences settings;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getActionBar().setTitle("SETTING");
setContentView(R.layout.setting);
check =(CheckBox)findViewById(R.id.checkBox);
check.setOnClickListener(this);
settings = Activty2.this.getSharedPreferences("checkBox",0);
Boolean a = settings.getBoolean("isChecked", false);
check.setChecked(a);
}
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.checkBox:
SharedPreferences.Editor editor = settings.edit();
if(!check.isChecked())
{
check.setChecked(true);
editor.putBoolean("isChecked", true);
editor.commit();
}
else
{
check.setChecked(false);
editor.putBoolean("isChecked", false);
editor.commit();
}
break;
}
}
public boolean getCheckStatus()
{
SharedPreferences mysettings;
try{
mysettings = Activity2.this.getSharedPreferences("checkBox",0);
}catch(NullPointerException e)
{
return false;
}
return mysettings.getBoolean("isChecked", false);
}
}
活动2仅用于显示是否选中了复选框,如果选中,则显示,如果有任何更改,则使用SharedReferences保存
我捕获NullPointerException的原因是,因为我在activity1中显示了toast,这是我的主要活动,所以当应用程序第一次运行时,在我运行activity2的onCreate()
之前,不会有SharedRef。因此,如果我遇到异常,则表示该应用程序是第一次运行,或者用户直到现在才打开Activity2。如果是,则返回默认的false值,如果不是,则永远不会调用catch()
,并且我会存储该值
我的问题:-
正如你所猜测的,它不起作用。复选框工作正常,每当我打开Activity2时,它都会显示上一个设置值,但toast总是显示而不是checkd
请告诉我,这里有什么错误,或者是否有其他更简单的方法来保存android应用程序用户设置并在应用程序的任何其他地方访问它们
谢谢。您应该在其他活动(打开共享首选项)中执行相同的操作,并检查您的复选框是否已设置:
settings = Activity.this.getSharedPreferences("checkBox",0);
Boolean a = settings.getBoolean("isChecked", false);
您应该在其他活动(打开共享首选项)中执行相同的操作,并检查您的复选框是否已设置:
settings = Activity.this.getSharedPreferences("checkBox",0);
Boolean a = settings.getBoolean("isChecked", false);
您应该在其他活动(打开共享首选项)中执行相同的操作,并检查您的复选框是否已设置:
settings = Activity.this.getSharedPreferences("checkBox",0);
Boolean a = settings.getBoolean("isChecked", false);
您应该在其他活动(打开共享首选项)中执行相同的操作,并检查您的复选框是否已设置:
settings = Activity.this.getSharedPreferences("checkBox",0);
Boolean a = settings.getBoolean("isChecked", false);
首先,将SharedPrefs read移动到activity1,如下所示
if(新活动2().getCheckStatus())
如果活动2更大,将会致命
因此:
public boolean getCheckStatus()
{
SharedPreferences mysettings;
try{
mysettings = getSharedPreferences("checkBox",0);
}catch(NullPointerException e)
{
return false;
}
return mysettings.getBoolean("isChecked", false);
}
转到活动1。Albo,将复选框“
设为常量值(公共静态最终字符串)
现在试试。首先,将SharedPrefs读取移动到activity1,如下所示
if(新活动2().getCheckStatus())
如果活动2更大,将会致命
因此:
public boolean getCheckStatus()
{
SharedPreferences mysettings;
try{
mysettings = getSharedPreferences("checkBox",0);
}catch(NullPointerException e)
{
return false;
}
return mysettings.getBoolean("isChecked", false);
}
转到活动1。Albo,将复选框“
设为常量值(公共静态最终字符串)
现在试试。首先,将SharedPrefs读取移动到activity1,如下所示
if(新活动2().getCheckStatus())
如果活动2更大,将会致命
因此:
public boolean getCheckStatus()
{
SharedPreferences mysettings;
try{
mysettings = getSharedPreferences("checkBox",0);
}catch(NullPointerException e)
{
return false;
}
return mysettings.getBoolean("isChecked", false);
}
转到活动1。Albo,将复选框“
设为常量值(公共静态最终字符串)
现在试试。首先,将SharedPrefs读取移动到activity1,如下所示
if(新活动2().getCheckStatus())
如果活动2更大,将会致命
因此:
public boolean getCheckStatus()
{
SharedPreferences mysettings;
try{
mysettings = getSharedPreferences("checkBox",0);
}catch(NullPointerException e)
{
return false;
}
return mysettings.getBoolean("isChecked", false);
}
转到活动1。Albo,将复选框“
设为常量值(公共静态最终字符串)
立即尝试。您不应使用新运算符构造新活动。曾经应始终使用意图创建活动。不应使用新运算符构造新活动。曾经应始终使用意图创建活动。不应使用新运算符构造新活动。曾经应始终使用意图创建活动。不应使用新运算符构造新活动。曾经它应该始终使用意图创建。