我正在创建一个程序,我想在其中使用一个活动作为启动画面,它显示我用作背景图像,复选框和按钮的指令列表。我希望在单击复选框或onChecked然后单击按钮时,启动时不应再次看到该活动。 我无法弄清楚如何实现代码。
这就是我正在做的但仍然没有用
CheckBox cb;
SharedPreferences sp;
Button btn;
int result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
cb = (CheckBox) findViewById(R.id.checkBox1);
sp = (SharedPreferences) PreferenceManager
.getDefaultSharedPreferences(this);
OnCheckedChangeListener cb1 = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(cb.isChecked()){
SharedPreferences.Editor editor = sp.edit();
editor.putInt("showActivity", 0);
editor.commit();
}
else{
result = sp.getInt("showActivity", -1);
if(result == 0){
}
}
}
};
cb.setOnCheckedChangeListener(cb1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "dgdgd", Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setClass(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
}
答案 0 :(得分:0)
如果单击复选框,则单击“确定”按钮时添加此项
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("showActivity", 0);
editor.commit();
当您启动应用时,请检查此类..
int result;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
result = sharedPreferences.getInt("showActivity", -1);
if(result == 0)
// do not show the activity.. start the next activity
答案 1 :(得分:0)
假设您正在查看天气预报XYZActivity
。
只需点击按钮
即可SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("showActivity",cb.isChecked());
editor.commit();
下次在开始XYZActivity之前检查。
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
if(sharedPreferences.getBoolean("showActivity",false)){
//check box is checked (no need to show activity)
}else{
//show activity here
}