如何在android中的登录页面中使用共享首选项概念

时间:2012-04-19 12:38:03

标签: android

当我点击登录按钮时输入用户名和密码表示登录将成功。 当我在点击复选框时提供用户名和密码,如果我成功登录后立即关闭应用程序,如果我再次打开应用程序用户名和密码必须是他们的文本框。现在我直接点击登录按钮,它必须完全登录成功。

我试试这段代码

public class Login extends Activity {
    CheckBox check;

    private static final String UPDATE_URL = "http://202.62.91.45/NewCozyDine/login1.php3";
    public ProgressDialog progressDialog;
    private EditText UserEditText;
    private EditText PassEditText;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        UserEditText = (EditText) findViewById(R.id.username);
        PassEditText = (EditText) findViewById(R.id.password);
        check=(CheckBox) findViewById(R.id.checkbox);
        Button button = (Button) findViewById(R.id.okbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int usersize = UserEditText.getText().length();
                int passsize = PassEditText.getText().length();
                if(usersize > 0 && passsize > 0) {
                    progressDialog.show();
                    String user = UserEditText.getText().toString();
                    String pass = PassEditText.getText().toString();
                    doLogin(user, pass);
                } else createDialog("Error","Please enter Username and Password");
            }
        });

我写了xml解析器的登录按钮(从服务器数据库验证)。怎么能写为复选框记住

4 个答案:

答案 0 :(得分:1)

试试这个,

 public SharedPreferences prefs;
 public String prefName = "MyPref";
 private static final String REMEMBER = "remember_me";
 public static final String Password = "volume_range";
 public static final String User_Name = "my_alarmstate";
 prefs = getSharedPreferences(prefName, MODE_PRIVATE);      
 remember_state  = prefs.getBoolean(REMEMBER, false);   
 remember_chebox.setChecked(remember_state);
 if(remember_state == true) {

            uname_et.setText(prefs.getString(User_Name, null));
            password_et.setText(prefs.getString(Password, null));
        }

SharedPreferences.Editor editor = prefs.edit();


                editor.putString(User_Name, entered_name);
                editor.putString(Password, entered_password);   
                editor.putBoolean(REMEMBER, remember_state);
                editor.commit();   

答案 1 :(得分:1)

在Checkbox Selected上识别它。在Listner上,您必须使用SharedPreference,这会将您的数据保存在应用程序中。

CheckBox savepass;
public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
private static final String PREF_STATUS = "status";

savepass = (CheckBox) findViewById(R.id.remeber);
savepass.setChecked(false);

//在登录按钮上编写此代码。此代码用于在SharedPreference中保存

if (savepass.isChecked()) {
                getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit()
                        .putString(PREF_USERNAME, uid.getText().toString())
                        .putString(PREF_PASSWORD, pwd.getText().toString())
                        .putString(PREF_STATUS, "true").commit();
                /*
                 * Toast.makeText(getApplicationContext(),
                 * "Saved Successfully", Toast.LENGTH_LONG).show();
                 */
            }else {
                getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit()
                        .putString(PREF_USERNAME, "").putString(
                                PREF_PASSWORD, "").putString(PREF_STATUS,
                                "").commit();
//this else part is if user has already been checked and saved in application, then name of the user will be displayed on username and password edittextbox.
            }

答案 2 :(得分:1)

if (successful login and remember-me checkbox is marked...) {
    SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    SharedPreferences.Editor spEditor = SP.edit();
    spEditor.putString("username"),userName);
    spEditor.putString("password",password);
    spEditor.commit();

然后on onCreate你只是检查它是否已经存在并且如果它存在则显示它。

SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    userName = SP.getString("username", "none");
if (!userName.equal("none"))
    editText.setText(userName);
....

答案 3 :(得分:0)

当您即将在您的应用程序中使用SharedPreferences时。我希望您查看SharedPreferences的这一个SO链接。

转到Saving Data in SharedPreferences。我希望您使用加密密码。

有关在Android中使用AES加密的指南,请参阅此

http://developer.motorola.com