package com.example.sharedpreferences;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText numbuter,password;
CheckBox checkBox;
SharePreferences a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numbuter=(EditText)findViewById(R.id.numbuter);
password=(EditText)findViewById(R.id.password);
checkBox=(CheckBox)findViewById(R.id.checkbox);
a=new SharePreferences();
numbuter.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s,int start,int before,int count){
String number=numbuter.getText().toString();
String pwd=a.duqu(MainActivity.this,number);
boolean b=a.duqucheck(MainActivity.this,number);
if(pwd!=null&&b!=false){
password.setText(pwd);
}
else
password.setText("");
}
@Override
public void afterTextChanged(Editable s)
{
}
@Override
public void beforeTextChanged(CharSequence s,int start,int count,int after)
{
}
}
);
}
public void button(View view)
{
String numb=numbuter.getText().toString();
String pass=password.getText().toString();
String pwd=a.duqu(MainActivity.this,numb);
if(numb.equals("")||pass.equals("")){
Toast.makeText(MainActivity.this, "密码或账号不能为空!", Toast.LENGTH_SHORT).show();
}
else if(!pwd.equals("pass"))
{
Toast.makeText(MainActivity.this,"账号或密码错误!",Toast.LENGTH_LONG).show();
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
else if(pwd.equals("pass"))
{
Toast.makeText(MainActivity.this,"登录成功!",Toast.LENGTH_LONG).show();
}
}
public void button2(View view)
{
String numb=numbuter.getText().toString();
String pass=password.getText().toString();
if(pass.equals("")||numb.equals("")){
Toast.makeText(MainActivity.this,"账号或密码不能为空",Toast.LENGTH_LONG).show();
}
else if(!pass.equals("")&&!numb.equals("")&&checkBox.isChecked())
{
a.baocun(MainActivity.this,numb,pass);
Toast.makeText(MainActivity.this,"注册成功",Toast.LENGTH_LONG).show();
numbuter.setText("");
password.setText("");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.sharedpreferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SharePreferences {
public boolean baocun(Context context,String numbuter,String password)
{
SharedPreferences sp=context.getSharedPreferences("date",Context.MODE_PRIVATE);
Editor editor=sp.edit();
editor.putString("username",numbuter);
editor.putString("pwd",password);
editor.commit();
return true;
}
public String duqu(Context context,String numbuter)
{
SharedPreferences sp=context.getSharedPreferences("date",Context.MODE_PRIVATE);
String password=sp.getString("pwd","");
if(password!=null)
{
return password;
}
else
{
return null;
}
}
public void baocuncheck(Context context,String number,boolean bool)
{
SharedPreferences sp=context.getSharedPreferences("date",Context.MODE_APPEND);
SharedPreferences.Editor editor=sp.edit();
editor.putBoolean(number,bool);
editor.commit();
}
public boolean duqucheck(Context context,String number)
{
SharedPreferences sp=context.getSharedPreferences("date",Context.MODE_APPEND);
boolean b=sp.getBoolean(number,false);
return b;
}
}