记住我的登录复选框的Andr​​oid [英] Keep me logged in CheckBox Android

查看:150
本文介绍了记住我的登录复选框的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造我所创建的登录和注册页面的Andr​​oid应用程序。
我想实现在登录页面的功能保持我登录。
我想我知道该怎么做,但几乎所有的东西我尝试是给一个错误。

I am creating an android app for which I have created login and signup pages. I want to implement the "keep me logged in" functionality in the login page. I think I know what to do but almost anything I try is giving an error.

我要复选框的状态存储在共享preference,然后用一个if else语句使用它,如果它被选中将跳过登录活动,并启动的主要活动,否则登录活动。

I have to store the state of checkbox in a SharedPreference and then use it with an if else statement, if it is checked it will skip the login activity and start the main activity, otherwise the login activity.

这将是有益的,如果有人可以提供样本code。

It would be helpful if someone could provide a sample code.

ChechBox keeplog = (CheckBox)findBiewById(R.Id.checkbox1)

我怎么能存储在共享preference以及如何在布尔或字符串的形式来获取它的状态,以便它可以用if-else语句中使用的检查状态?

How can I store the check state in Shared Preference and how to fetch its state in the form of boolean or string so that it can be used with if-else statement?

推荐答案

请尝试以下方法,声明一个布尔变量。

Try the following way, declare one boolean variable.

CheckBox keeplog = (CheckBox) findViewById(R.id.checkBox1);
boolean isChecked = false;

然后共享preferences复选框的状态存储与 CheckedChangeListener

keeplog.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("isChecked", isChecked);
        editor.commit();
    }
});    

然后现在从共享preferences 使用方法如下:

SharedPreferences settings1 = getSharedPreferences("PREFS_NAME", 0);
isChecked = settings1.getBoolean("isChecked", false);

现在的if语句检查,并开始像下面这样的活动

Now check with the if statement and start your activity like the following

if (isChecked) {
    Intent i = new Intent(MainActivity.this, ThirdActivity.class);
    startActivity(i);
} else {
    Intent i = new Intent(MainActivity.this, SecondActivity.class);
    startActivity(i);
}

感谢您。

这篇关于记住我的登录复选框的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆