1.界面布局(layout.xml)
采用线性布局的方式,先设置了一个 Image View 组件显示头像,然后放置了两个 EditText 和两个按钮,最后写了自定义样式的文件来设置 EditText 的样式。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/ic_launcher_background" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/zh"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:hint="账号"
android:gravity="center"
android:layout_height="40dp" />
<EditText
android:id="@+id/mm"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:hint="密码"
android:gravity="center"
android:layout_height="40dp"
android:inputType="numberPassword" />
<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:background="#3F51B5"
android:textColor="#FFFFFF"
android:text="注册" />
<Button
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:background="#3F51B5"
android:textColor="#FFFFFF"
android:text="显示保存的信息" />
</LinearLayout>
</LinearLayout>
2. 编写 MainActivity 代码
编写 MainActivity 中的代码,首先对两个 EditText 和两个按钮组件进行初始化,然后对按钮添加监听事件。
第一个按钮的监听事件负责把用户输人的用户名和密码保存在 user mes .xml 文件中;
第二个按钮的监听事件负责把数据从文件中读取出来,然后显示给用户
public class MainActivity extends Activity implements View.OnClickListener {
private EditText zh,mm;
private Button register_b,show_b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init(); }
private void init() {
zh=(EditText)findViewById(R.id.zh);
mm=(EditText)findViewById(R.id.mm);
register_b=(Button) findViewById(R.id.register);
show_b=(Button) findViewById(R.id.show);
register_b.setOnClickListener(this);
show_b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register:
String zh_str=zh.getText().toString();
String mm_str=mm.getText().toString();
boolean flag=save_userMes(MainActivity.this,zh_str,mm_str);
if(flag){
Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"保存失败",Toast.LENGTH_SHORT).show();
}
break;
case R.id.show:
Map<String,String>user=getuser_mes(MainActivity.this);
if(user!=null){
String zh=user.get("zh");
String mm=user.get("mm");
Toast.makeText(MainActivity.this,"用户名:"+zh+"\n"+"密码:"+mm,
Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
private boolean save_userMes(Context context, String zh, String mm) {
SharedPreferences sharedPreferences=context.getSharedPreferences("user_mes",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("zh",zh);
editor.putString("mm",mm);
editor.commit();
return true;
}
//从user_mes.xml中取出用户名和密码的方法
private Map<String, String> getuser_mes(Context context) {
SharedPreferences sharedPreferences=context.getSharedPreferences("user_mes",MODE_PRIVATE);
String zh=sharedPreferences.getString("zh",null);
String mm=sharedPreferences.getString("mm",null);
Map<String,String>user=new HashMap<String, String>();
user.put("zh",zh);
user.put("mm",mm);
return user;
}
}
上述代码包含了两个方法:一个是 save _ userMes 方法用来保存用户输人的信息,另一个是 getuser_mes ,它添加了验证,判断是否获取到用户信息,如果获取到则显示给用户。
3.运行程序
完成以上的编码以后,运行程序,首先输人用户名和密码,然后单击“注册”按钮,看是否提示用户,最后单击“显示保存的信息”按钮看是否有信息显示。