public class MainActivity extends Activity {
EditText username, password;
Button registration;
SharedPreferences sp;
Editor editor;
String name;
String pass;
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControl();
//得到SharedPreferences,会生成user.xml
sp = this.getSharedPreferences("user", 0);
editor = sp.edit();
registration.setOnClickListener(new ButtonListener());
}
/**
* 根据ID得到控件
*/
private void initControl(){
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
registration = (Button) findViewById(R.id.registration);
}
/**
* 按钮的监听类
* @author symbolDelia
*
*/
private class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
name = username.getText().toString();
pass = password.getText().toString();
//下面三行是将数据写入user.xml中
editor.putString("username", name);
editor.putString("password", pass);
editor.commit();
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
//写入xml之后,将EditText设为空
username.setText("");
password.setText("");
//跳转页面
intent = new Intent();
intent.setClass(MainActivity.this, Land.class);
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
tools:context=".MainActivity" />
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:hint="@string/username"
android:inputType="text"
>
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/username"
android:layout_marginTop="24dp"
android:text="@string/password" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/username"
android:ems="10"
android:inputType="textPassword"
android:hint="@string/password"
/>
<Button
android:id="@+id/registration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/password"
android:layout_marginLeft="24dp"
android:layout_marginTop="40dp"
android:text="@string/registration" />
</RelativeLayout>
public class Land extends Activity {
EditText username, password;
Button land;
String editname, editpass;
String name, pass;
SharedPreferences shared;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.land);
initControl();
//得到SharedPreferences,这里是得到之前的user.xml
shared = this.getSharedPreferences("user", 0);
//得到user.xml中username和password键的对应值
name = shared.getString("username", null);
pass = shared.getString("password", null);
//监听
land.setOnClickListener(new LandListener());
}
/**
* 根据ID得到控件
*/
private void initControl(){
username = (EditText) findViewById(R.id.landusername);
password = (EditText) findViewById(R.id.landpassword);
land = (Button) findViewById(R.id.land);
}
/**
* 按钮的监听类
* @author symbolDelia
*
*/
private class LandListener implements OnClickListener{
@Override
public void onClick(View v) {
//得到EditText里的内容
editname = username.getText().toString();
editpass = password.getText().toString();
//比较EditText里的值是否与SharedPreferences中得到的值一样
if(editname.equals(name) && editpass.equals(pass)){
Toast.makeText(Land.this, "登陆成功", Toast.LENGTH_LONG).show();
intent = new Intent();
intent.setClass(Land.this, GetUserInformation.class);
startActivity(intent);
}else{
Toast.makeText(Land.this, "用户名或密码不不正确", Toast.LENGTH_LONG).show();
}
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="23dp"
android:layout_marginTop="157dp"
android:text="@string/username" />
<EditText
android:id="@+id/landusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="text"
android:hint="@string/username"
>
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/password" />
<EditText
android:id="@+id/landpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/landusername"
android:ems="10"
android:inputType="textPassword"
android:hint="@string/password"/>
<Button
android:id="@+id/land"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/landpassword"
android:layout_marginTop="70dp"
android:layout_toRightOf="@+id/textView2"
android:text="@string/land" />
</RelativeLayout>
获取用户信息页面:
public class GetUserInformation extends ListActivity {
SharedPreferences shared;
String username, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//得到SharedPreferences,这里是得到之前的user.xml
shared = this.getSharedPreferences("user", 0);
//得到user.xml中username和password键的对应值
username = shared.getString("username", null);
password = shared.getString("password", null);
//设置适配器
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.getuserinformation,
new String[]{"name", "pass"},
new int[]{R.id.getusername, R.id.getpassword}
);
setListAdapter(adapter);
}
//得到数据
private List<Map<String, String>> getData(){
List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
Map<String, String> data = new HashMap<String, String>();
data.put("name", username);
data.put("pass", password);
datas.add(data);
return datas;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/getusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:text="@string/username" />
<TextView
android:id="@+id/getpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/getusername"
android:layout_alignBottom="@+id/getusername"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"
android:text="@string/password" />
</RelativeLayout>
string.xml:
<resources>
<string name="app_name">MySharedPreferences</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="username">用户名:</string>
<string name="password">密 码:</string>
<string name="registration">注册</string>
<string name="getuserinformation">获取用户信息</string>
<string name="land">登陆</string>
<string name="registrationpage">注册界面</string>
<string name="getuserinformationpage">获取用户信息界面</string>
<string name="landpage">登陆界面</string>
</resources>