制作一个登录界面,以SP方式存储用户名。用户下次登录时自动显示上次填写的用户名

制作一个登录界面,以SP方式存储用户名。用户下次登录时自动显示上次填写的用户名

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.JiyidengluActivity"
11     android:orientation="vertical">
12 
13     <EditText
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:hint="用户名"
17         android:id="@+id/et_1"/>
18     <EditText
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:hint="登录密码"
22         android:id="@+id/et_2"
23         android:inputType="textPassword"/>
24 
25     <LinearLayout
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content">
28 
29         <Button
30             android:layout_width="match_parent"
31             android:layout_height="wrap_content"
32             android:text="登陆"
33             android:onClick="bt2_OnClick"/>
34     </LinearLayout>
35 
36 </LinearLayout>

 

 1 package com.hanqi.testapp3;
 2 
 3 import android.content.SharedPreferences;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.EditText;
 8 import android.widget.Toast;
 9 
10 public class JiyidengluActivity extends AppCompatActivity {
11 
12     EditText et_1;
13     EditText et_2;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_jiyidenglu);
19 
20         et_1=(EditText)findViewById(R.id.et_1);
21         et_2=(EditText)findViewById(R.id.et_2);
22 
23         //
24         SharedPreferences sp=getSharedPreferences("defg",MODE_APPEND);
25 
26         String str=sp.getString("d",null);
27 
28         et_1.setText(str);
29     }
30 
31     public void bt2_OnClick(View v)
32     {
33         EditText et_1=(EditText)findViewById(R.id.et_1);
34 
35         String yonghuming=et_1.getText().toString();
36 
37         if (yonghuming==null||yonghuming.trim().length()==0)
38         {
39             Toast.makeText(JiyidengluActivity.this, "请输入正确的用户名", Toast.LENGTH_SHORT).show();
40 
41             return;
42         }
43 
44         else {
45 
46             //提取保存
47             SharedPreferences sharedPreferences = getSharedPreferences("defg", MODE_APPEND);
48 
49             SharedPreferences.Editor editor = sharedPreferences.edit();
50 
51             editor.putString("d",yonghuming);
52 
53             editor.commit();
54 
55             Toast.makeText(JiyidengluActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
56         }
57     }
58 }

   

   

   

posted @ 2016-05-23 21:14  烟_雨_江_南  阅读(1070)  评论(0编辑  收藏  举报