两个单选按钮是否保留用户的凭据

时间:2011-09-06 07:56:22

标签: android sharedpreferences

  

可能重复:
  I'm working on having a "Keep me on Logged in" state on my app. How should i do it?

当用户检查单选按钮时,当前代码正常工作,我想要的是什么。当用户重新打开应用程序时,我希望应用程序返回登录屏幕。

这是java文件:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity);

        final RadioButton radio_on = (RadioButton) findViewById(R.id.on);
        final RadioButton radio_off = (RadioButton) findViewById(R.id.off);

        SharedPreferences pref = getSharedPreferences("LoggedIn", MODE_PRIVATE);
        final SharedPreferences.Editor prefEdit = pref.edit();

        radio_on.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                prefEdit.putBoolean("booleanValue", isChecked).commit();
          }
     });

2 个答案:

答案 0 :(得分:0)

如上一个问题,您的解决方案太接近您的解决方案了 试试这个

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loginactivity);

    final RadioButton radio_on = (RadioButton) findViewById(R.id.on);
    final RadioButton radio_off = (RadioButton) findViewById(R.id.off);

    SharedPreferences pref = getSharedPreferences("LoggedIn", MODE_PRIVATE);
    final SharedPreferences.Editor prefEdit = pref.edit();

    if(pref.getBoolean("booleanValue", "")){
       intent = new Intent(getApplicationContext(),HomeActivity.class);
       startActivity(intent);
       finish(); 
    }

    radio_on.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            prefEdit.putBoolean("booleanValue", isChecked);
            prefEdit.commit();
      }
 });

答案 1 :(得分:0)

根据我的理解,当用户进入第一个活动(loginactivity)时,如果他选择自动登录,当他再次打开应用程序时,他就会进入并且无法更改他的选项,而不是登录,因为他从来没有进入那个屏幕。 如果这是正确的,这是应用程序设计的问题。您可以在应用中添加一个设置,让您更改选项。