我正在制作一个我想使用Activity
的程序
作为一个闪屏,显示一个指令列表
我用作背景图像,复选框和按钮。
当CheckBox
为clicked
或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()){
}
else{
result = sp.getInt("showActivity", -1);
if(result == 0){
Intent i = new Intent();
i.setClass(MainActivity.this, SecondActivity.class);
startActivity(i);
}
}
}
};
cb.setOnCheckedChangeListener(cb1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("showActivity",cb.isChecked());
editor.commit();
Toast.makeText(getApplicationContext(), "dgdgd", Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setClass(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
}
答案 0 :(得分:1)
基本上你需要使用Share-Preference
来实现,将条件置于on user checked initially or not
的onCreate中,并按照布尔值调用适当的屏幕。请尝试以下代码。
public class SplashScreen extends Activity {
SharedPreferences pref;
boolean state;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pref = getSharedPreferences("PackageName", Context.MODE_PRIVATE);
state= pref.getBoolean("State", false);
if(state){
Intent intent= new Intent(SplashScreen.this,NextActivity.class);
startActivity(intent);
finish();
}else{
setContentView(R.layout.fragment_main);
CheckBox chk = (CheckBox)findViewById(R.id.checkBox1);
Button btn =(Button)findViewById(R.id.button1);
chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
state=true;
}else{
state=false;
}
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Editor edit= pref.edit();
edit.putBoolean("State", state);
edit.commit();
Intent intent= new Intent(SplashScreen.this,NextActivity.class);
startActivity(intent);
}
});
}
}
}
答案 1 :(得分:0)
CheckBox cb;
SharedPreferences sp;
Button btn;
// never used
//int result;
private final String DONT_SHOW_ACTIVITY = "dontShowActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = (SharedPreferences) PreferenceManager
.getDefaultSharedPreferences(this);
// default value is false, means you will start MainActivity at first time.
// And if you hava checked "dont show again", MainActivity will finish itself
// immediately, and start the SecondActivity.
if(sp.getBoolean(DONT_SHOW_ACTIVITY, false)){
Intent i = new Intent();
i.setClass(MainActivity.this, SecondActivity.class);
startActivity(i);
this.finish();
}
setContentView(R.layout.activity_main);
cb = (CheckBox) findViewById(R.id.checkBox1);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(DONT_SHOW_ACTIVITY,cb.isChecked());
editor.commit();
Toast.makeText(getApplicationContext(), "dgdgd", Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setClass(MainActivity.this, SecondActivity.class);
startActivity(i);
// you should finish MainActivity here, or you can press back return from
// SecondActivity to MainActivity.
this.finish()
}
});
}