1. import shared preference
android.content.SharedPreferences
2. declare all field inside the mainactivity class.
3. initialize all field inside the oncreate function:
4. Define one function outside the oncreate function but inside the mainactivity class which store the data:
5. call the function in the setOnClickLister event of button.
android.content.SharedPreferences
2. declare all field inside the mainactivity class.
EditText user, pass; Button login; SharedPreferences sharedPreferences; SharedPreferences.Editor editor; Boolean savelogin; CheckBox savelogincheckbox;
3. initialize all field inside the oncreate function:
user=(EditText)findViewById(R.id.txtuser); pass=(EditText)findViewById(R.id.txtpass); login=(Button)findViewById(R.id.btnlogin); sharedPreferences = getSharedPreferences("loginref",MODE_PRIVATE); savelogincheckbox=(CheckBox)findViewById(R.id.checkBox); editor=sharedPreferences.edit();
4. Define one function outside the oncreate function but inside the mainactivity class which store the data:
public void login(){ String usrname = user.getText().toString(); String passwrd = pass.getText().toString(); if(usrname.equals("TechSupportNep") && passwrd.equals("TechSupportNep")){ Toast.makeText(this,"username and passwrod matched!",Toast.LENGTH_LONG).show(); if(savelogincheckbox.isChecked()){ editor.putBoolean("savelogin",true); editor.putString("username",usrname); editor.putString("password",passwrd); editor.commit(); } }else { Toast.makeText(this, "error",Toast.LENGTH_LONG).show(); } }
5. call the function in the setOnClickLister event of button.
login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } });
6. retrieve the store data:
savelogin=sharedPreferences.getBoolean("savelogin",true); if(savelogin==true){ user.setText(sharedPreferences.getString("username",null)); pass.setText(sharedPreferences.getString("password",null));
No comments:
Post a Comment