安卓:问题记得我登录活动的功能 [英] Android:problem in remember me functionality of login Activity

查看:89
本文介绍了安卓:问题记得我登录活动的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2的EditText和复选框,并登录按钮来创建登录页面。 如果我设置复选框,启用我想保存数据,以便下一次用户并不需要填写的字段..
我已经使用这个code,但没有运气。

i have create login page with 2 EditText and checkbox and login button. if i set checkbox to Enabled i want to save data so next time user doesn't need to fill that fields..
i have uses this code but no luck..

公共类LoginPage延伸活动{

public class LoginPage extends Activity {

EditText d_ID;
EditText password;
CheckBox cb;
ImageButton ib;

public static final String PREFS_NAME = "MyPrefsFile";
public String PREFS_USER;
public String PREFS__PASS;
String username;
String upass;

共享preferences preF

SharedPreferences pref

@Override
public void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.loginpage);

    d_ID = (EditText) findViewById(R.id.dulzuID);
    password = (EditText) findViewById(R.id.dulzuPASS);

    cb = (CheckBox) findViewById(R.id.remember);

    ib = (ImageButton) findViewById(R.id.login);






        pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        username = pref.getString(PREFS_USER, null);
        upass = pref.getString(PREFS__PASS, null);
        d_ID.setText(username);
        password.setText(upass);




    ib.setOnClickListener(new OnClickListener() {

        public void onClick(View view) {

            startActivity(new Intent(LoginPage.this, Features.class));
        }
    });

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton cb1, boolean bln) {

                PREFS_USER = d_ID.getText().toString();//get user name from EditText
                PREFS__PASS = password.getText().toString();//get user Password from EditText
                getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().putString(PREFS_USER, username).putString(PREFS__PASS, upass).commit();

        }
    });

}

}

任何帮助??

谢谢...

推荐答案

我想你混淆了获取用户密码的值的变量,并且确定在preferences的用户名/密码值的变量。我的认为的,你打算这些:

I think you are confusing the variables for retrieving the value of the users password and the variables that identify the username/password values in the preferences. I think that you intend these:

public String PREFS_USER;
public String PREFS__PASS;

是标识符存储的用户名和密码,但是你再设置他们是的你已经从拉相应的 EditTexts 。我已经改写了一些$ C $下您:

to be the identifiers for your stored username and password, however you then set them to be the values that you have pulled from the corresponding EditTexts. I have rewritten some of the code for you:

public static final String PREFS_NAME = "MyPrefsFile";
public static final String PREFS_USER = "prefsUsername";
public static final String PREFS__PASS = "prefsPassword";
...
pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
username = pref.getString(PREFS_USER, "");
upass = pref.getString(PREFS__PASS, "");
...
public void onCheckedChanged(CompoundButton cb1, boolean bln) {
    username = d_ID.getText().toString();//get user name from EditText
    upass = password.getText().toString();//get user Password from EditText
    getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().putString(PREFS_USER, username).putString(PREFS__PASS, upass).commit();
}

就个人而言,我不会做那样的虽然。我会检查复选框的值,当用户提交表单,并且只保存用户名和放大器;密码在这一点上。如果用户取消选中什么,然后重新检查的复选框的之前他们已经进入了自己的密码?您将节省空值和惹恼你的用户。

Personally, I wouldn't do it like that though. I would check the value of the checkbox when the user submits the form, and only save the username & password at that point. What if the user unchecks and then rechecks the tick box before they have entered their password? You will save empty values and annoy your users.

这篇关于安卓:问题记得我登录活动的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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