Android CheckBox OnCheckListener不起作用

时间:2017-08-02 06:37:55

标签: java android android-checkbox

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if (b){
                editText.setText(nm);
                editText1.setText(ps);
                Toast.makeText(Page1.this,"Checkbox Remember me is " + String.valueOf(b),Toast.LENGTH_LONG).show();
            }
        }
    });

我正在尝试创建一个包含两个EditTexts和一个CheckBox的登录页面。我正在尝试在选中CheckBox时保存sharedprefrence中的登录详细信息。在我的代码中,我将OnCheckedListner设置为知道是否已检查。但是布尔值b总是正确的......为什么? 请帮帮我!

第1页活动的完整代码:

public class Page1 extends AppCompatActivity {

Button button;
EditText editText,editText1;
CheckBox checkBox;
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page1);

    button = (Button)findViewById(R.id.bt1);
    editText = (EditText)findViewById(R.id.et1);
    editText1 = (EditText)findViewById(R.id.et2);
    checkBox = (CheckBox)findViewById(R.id.cb1);

    sharedPreferences = getSharedPreferences("MYSP", MODE_PRIVATE);
    final String nm = sharedPreferences.getString("uname", "");
    final String ps = sharedPreferences.getString("upass", "");
    final boolean saveLogin = sharedPreferences.getBoolean("save",false);
    final boolean logout = sharedPreferences.getBoolean("logout", false);

    if (!logout){
        Intent intent = new Intent(Page1.this, ProfilePage.class);
        startActivity(intent);
    }

    final boolean[] isChecked = new boolean[1];

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if (b){
                editText.setText(nm);
                editText1.setText(ps);
                Toast.makeText(Page1.this,"Checkbox Remember me is "+ String.valueOf(b) ,Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(Page1.this,"Checkbox Remember me is "+ String.valueOf(b),Toast.LENGTH_LONG).show();
            }
        }
    });

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = editText.getText().toString();
            String pass = editText1.getText().toString();
            SharedPreferences.Editor editor = sharedPreferences.edit();
            if (isChecked[0]) {
                editor.putString("uname", name);
                editor.putString("upass", pass);
                editor.putBoolean("save", true);
                editor.putBoolean("logout", false);
                editor.commit();
            }
            else {
                editor.putString("uname", name);
                editor.putString("upass", pass);
                editor.putBoolean("logout", false);
                editor.commit();
            }
            Intent intent = new Intent(Page1.this, ProfilePage.class);
            startActivity(intent);
        }
    });
}

}

ProfilePage活动代码:

public class ProfilePage extends AppCompatActivity {

Button button;
TextView textView;
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_page);

    button = (Button)findViewById(R.id.btn1);
    textView = (TextView)findViewById(R.id.tv1);
    sharedPreferences = getSharedPreferences("MYSP", MODE_PRIVATE);
    final String n = sharedPreferences.getString("uname", "");
    final String p = sharedPreferences.getString("upass", "");

    textView.setText("Welcome "+ n + "    ");

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            if (sharedPreferences.getBoolean("save", false)) {
                editor.putString("uname", n);
                editor.putString("upass", p);
                editor.putBoolean("logout", true);
                editor.commit();
            }
            else {
                editor.clear();
                editor.putBoolean("logout", true);
                editor.commit();
            }
            textView.setText("Logout Success!");
        }
    });
}

@Override
public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}

}

3 个答案:

答案 0 :(得分:1)

您的代码仅处理案例b = true。修改您的代码以包含else部分,如

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if (b){
                editText.setText(nm);
                editText1.setText(ps);
                Toast.makeText(Page1.this,"Checkbox Remember me is true",Toast.LENGTH_LONG).show();
            }
           else{
                Toast.makeText(Page1.this,"Checkbox Remember me is false",Toast.LENGTH_LONG).show();
           }
        }
    });

答案 1 :(得分:1)

这是xml中的复选框:

                      <CheckBox
                        android:id="@+id/remember_me_checkbox"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:buttonTint="@color/white"
                        android:gravity="left" />   

这是你的java代码:

private CheckBox remember_me_checkbox;
private boolean isRemembered; //false by default

    remember_me_checkbox = (CheckBox) findViewById(R.id.remember_me_checkbox);

    remember_me_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            isRemembered = isChecked;

        }
    });

每当用户成功登录时,您只需将其保存在首选项中。同时,您必须根据您的要求保存用户名和密码。

SharedPreferences pref 
 =getApplicationContext().getSharedPreferences(AppPreferences.PREF_FIREBASE_TOKEN, 0);
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean("remembered", isRemembered);
            editor.commit();

并且,下次您在退出时进入此屏幕。您将检查isRemembered是否为true,如果为true,则获取用户名和密码并将它们设置为相应的editTexts。

SharedPreferences pref = getApplicationContext()。getSharedPreferences(AppPreferences.PREF_FIREBASE_TOKEN,0);

boolean remembered = pref.getBoolean("remembered", false);

if(remembered ){

//get the username and password from pereference and set to editTexts.
}

答案 2 :(得分:0)

您不应该首先添加OnCheckChangeListener。单击“登录”按钮后,您应该已通过检查电子邮件和密码的所有验证以及“记住我”复选框的状态来保存SharedPreference中的凭据。

同样在onCreate中,您必须从SharedPreference中检查isRemember值,并显示相应的Edittext值。