【问题标题】:Using Shared preferenece , checkbox and button使用共享首选项、复选框和按钮
【发布时间】:2016-11-25 19:25:03
【问题描述】:

我正在制作一个程序,我想在其中使用Activity 作为显示指令列表的启动屏幕 我用作背景图像、复选框和按钮。 当CheckBoxclickedonChecked 和 然后我单击按钮,不应该看到该活动 再次在启动时。

这是我正在做的,但仍然没有用

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);

        }
    });
}

【问题讨论】:

  • 这是您的主要/初始活动?
  • 这是一个启动活动,我希望当用户单击复选框时,不应再次看到此活动,如果他不单击复选框,则应在启动时再次看到此活动
  • 但是你哪里有这个逻辑?可以发一下吗

标签: android button checkbox sharedpreferences


【解决方案1】:

基本上您需要使用Share-Preference 来实现,将条件放在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);


            }
        });

        }

    }



}

【讨论】:

  • 现在如果我想使用 Intent 返回 SplashScreen 活动,它不会发生??
  • 哦,如果未选中复选框,则用户可以返回启动画面,是吗?
  • 即使复选框被选中,我也想从另一个活动返回 SplashActivty。例如:我正在使用一个菜单,从那里我想去 SplashActivity 但是当应用程序启动时,如果它被选中,则启动时不应该看到 Splash 活动
  • 但是在这种情况下,一旦用户选中复选框,从下一次打开应用程序启动画面就不会显示给您。
  • 然后您需要从调用启动画面的位置(菜单)清除存储的首选项,以便显示启动画面并尝试理解代码并走得更远。
【解决方案2】:
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()
    }
});

}

【讨论】:

    猜你喜欢
    • 2011-12-18
    • 2014-10-26
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多