0

当我在单击登录按钮时提供用户名和密码时,意味着登录将成功。当我在单击复选框时提供用户名和密码时,如果我成功登录并且我现在关闭应用程序,如果我再次打开应用程序用户名和密码必须是他们在文本框中的密码。现在我直接点击登录按钮,它必须完全登录成功。

我试试这段代码

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

4 回答 4

1

只需在选中的复选框上识别它。在其 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.
            }
于 2012-04-19T12:48:22.493 回答
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();

然后在 onCreate 上,您只需检查它是否已经存在,如果存在则显示它。

SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    userName = SP.getString("username", "none");
if (!userName.equal("none"))
    editText.setText(userName);
....
于 2012-04-19T12:50:10.980 回答
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();   
于 2012-04-19T12:43:51.233 回答
0

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

转到此处在 SharedPreferences 中保存数据。我希望您对密码使用加密。

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

http://developer.motorola.com

于 2012-04-19T13:03:13.250 回答