2012-04-19 47 views
0

当我点击登录按钮时给予用户名和密码意味着登录将成功。 当我点击用户名和密码复选框后,如果我成功登录后,我现在关闭应用程序,如果我再次打开应用程序的用户名和密码必须是他们在文本框中。现在我直接点击登录按钮,它必须完全登录成功。如何在Android登录页面中使用共享偏好概念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解析器(从服务器数据库验证)登录按钮。如何写了复选框记得

+0

看看http://developer.android.com/reference/android/content/SharedPreferences.html。使用SharedPreferences存储您的凭证和复选框状态,并在下次调用时检索。 – Rajesh 2012-04-19 12:43:34

回答

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

刚刚确定的复选框即可。在其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. 
      } 
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); 
....