1

我正在创建一个已为其创建登录和注册页面的 android 应用程序。我想在登录页面中实现“保持登录”功能。我想我知道该怎么做,但几乎我尝试的任何事情都会出错。

我必须将复选框的状态存储在 SharedPreference 中,然后将其与 if else 语句一起使用,如果选中它将跳过登录活动并启动主要活动,否则将跳过登录活动。

如果有人可以提供示例代码会很有帮助。

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

如何将检查状态存储在 Shared Preference 中,以及如何以布尔值或字符串的形式获取其状态,以便与 if-else 语句一起使用?

4

2 回答 2

4

试试下面的方法,声明一个布尔变量。

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

然后将复选框的状态存储在共享首选项中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();
    }
});    

SharedPreferences然后现在使用以下方式获取复选框的状态。

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

现在检查 if 语句并开始您的活动,如下所示

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

谢谢你。

于 2013-07-06T06:45:14.180 回答
1

尝试以下操作,我已经粘贴了有关如何连接和登录到 ejabberd 服务器的代码,因为没有它,代码对于记住我的功能看起来不完整。我添加了解释如何在 SharedPrefernces 中存储和检索值的注释。

public class Login extends Activity
{
private XMPPConnection connection;
private ProgressBar progressBar;
private EditText userId;
private EditText password;
private CheckBox rememberMe;
private SharedPreferences sharedPreferences;
private Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    userId = (EditText) findViewById(R.id.user_id);
    password = (EditText) findViewById(R.id.password);
    rememberMe = (CheckBox) findViewById(R.id.remember_me);
    progressBar = (ProgressBar) findViewById(R.id.login_progress);
                //Retrieving SharedPreferences
    sharedPreferences = this.getPreferences(Context.MODE_PRIVATE);
                //Retrieving editor
    editor = sharedPreferences.edit();
                //Retrieving remember me checkbox's set value which if was stored in SharedPreferences
    if(sharedPreferences.getBoolean(getString(R.string.remember_me), false))
    {
        String uId, pass;
        uId = sharedPreferences.getString(getString(R.string.user_id), "");
        pass = sharedPreferences.getString(getString(R.string.password), "");
        userId.setText(uId);
        password.setText(pass);
        rememberMe.setChecked(sharedPreferences.getBoolean(getString(R.string.remember_me), false));
        new Connection().execute(uId, pass);
    }
}

public void login(View view)
{
    new Connection().execute(userId.getText().toString(), password.getText().toString());
}

private class Connection extends AsyncTask<String, Void, Integer>
{
    private static final int CONNECTION_FAILURE = 0;
    private static final int LOGIN_FAILURE = 1;
    private static final int SUCCESS = 2;

    protected void onPreExecute()
    {
        progressBar.setVisibility(ProgressBar.VISIBLE);
    }

    protected Integer doInBackground(String... strings)
    {
        ConnectionConfiguration conConfig = new ConnectionConfiguration("192.168.1.66", 5222, "domain");
        connection = new XMPPConnection(conConfig);
        try
        {
            connection.connect();
            Log.i("TESTING", "CONNECTED TO " + connection.getHost());
        }
        catch(Exception e)
        {
            Log.e("TESTING", e.getMessage());
            return CONNECTION_FAILURE;
        }
        try
        {
            connection.login(strings[0], strings[1]);
            Log.i("TESTING", "LOGGED IN AS " + connection.getUser());
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
        }
        catch(Exception e)
        {
            Log.e("TESTING", e.getMessage());
            return LOGIN_FAILURE;
        }
        return SUCCESS;
    }

    protected void onPostExecute(Integer integer)
    {
        progressBar.setVisibility(ProgressBar.GONE);
        switch(integer)
        {
            case CONNECTION_FAILURE:
                Toast.makeText(getApplicationContext(), "Failed to connect to the server.", Toast.LENGTH_LONG).show();
                break;
            case LOGIN_FAILURE:
                Toast.makeText(getApplicationContext(), "Please check your user id and or password.", Toast.LENGTH_LONG).show();
                break;
            case SUCCESS:
                if(rememberMe.isChecked())
                {
                                                        //Setting value in SharedPrefernces using editor.
                    editor.putBoolean(getString(R.string.remember_me), rememberMe.isChecked());
                    editor.putString(getString(R.string.user_id), userId.getText().toString());
                    editor.putString(getString(R.string.password), password.getText().toString());
                                                         //Committing the changes.
                    editor.commit();
                }
                else
                                                         //Clearing SharedPreferences.
                    sharedPreferences.edit().clear().commit();
                startActivity(new Intent(getApplicationContext(), Home.class));

        }
    }
}
}
于 2013-07-06T06:24:26.673 回答