主页面布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/activity_main" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context="com.example.asus.sharedpreferences.MainActivity"> 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:orientation="horizontal"> 14 <TextView 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_weight="2" 18 android:text="账号:"/> 19 <EditText 20 android:id="@+id/name" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:layout_weight="1" 24 android:hint="请输入"/> 25 </LinearLayout> 26 <LinearLayout 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:orientation="horizontal"> 30 <TextView 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:layout_weight="2" 34 android:text="密码:"/> 35 <EditText 36 android:id="@+id/password" 37 android:layout_width="match_parent" 38 android:layout_height="wrap_content" 39 android:layout_weight="1" 40 android:hint="请输入"/> 41 </LinearLayout> 42 <LinearLayout 43 android:layout_width="match_parent" 44 android:layout_height="match_parent" 45 android:orientation="horizontal"> 46 <Button 47 android:id="@+id/dl_btn" 48 android:layout_width="match_parent" 49 android:layout_height="wrap_content" 50 android:layout_weight="1" 51 android:text="登录"/> 52 <Button 53 android:id="@+id/zc_btn" 54 android:layout_width="match_parent" 55 android:layout_height="wrap_content" 56 android:layout_weight="1" 57 android:text="注册"/> 58 </LinearLayout> 59 </LinearLayout>
Activity:
1 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 2 3 private EditText name; 4 private EditText password; 5 private Button dl_btn; 6 private Button zc_btn; 7 private LinearLayout activity_main; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 initView(); 14 } 15 16 private void initView() { 17 name = (EditText) findViewById(R.id.name); 18 password = (EditText) findViewById(R.id.password); 19 dl_btn = (Button) findViewById(R.id.dl_btn); 20 zc_btn = (Button) findViewById(R.id.zc_btn); 21 activity_main = (LinearLayout) findViewById(R.id.activity_main); 22 23 dl_btn.setOnClickListener(this); 24 zc_btn.setOnClickListener(this); 25 //存储 26 SharedPreferences sp = getSharedPreferences("sp", 0); 27 if (sp!=null){ 28 //获取 29 String name1 = sp.getString("name", ""); 30 String password1 = sp.getString("password", ""); 31 //放入 32 name.setText(name1); 33 password.setText(password1); 34 } 35 36 } 37 38 @Override 39 public void onClick(View v) { 40 switch (v.getId()) { 41 case R.id.dl_btn: 42 43 break; 44 case R.id.zc_btn: 45 Intent intent = new Intent(this, Main2Activity.class);//跳转到注册页面 46 startActivity(intent); 47 break; 48 } 49 } 50 51 }
注册页面的布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/activity_main2" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context="com.example.asus.sharedpreferences.Main2Activity"> 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:orientation="horizontal"> 14 <TextView 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_weight="2" 18 android:text="账号:"/> 19 <EditText 20 android:id="@+id/name" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:layout_weight="1" 24 android:hint="请输入"/> 25 </LinearLayout> 26 <LinearLayout 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:orientation="horizontal"> 30 <TextView 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:layout_weight="2" 34 android:text="密码:"/> 35 <EditText 36 android:id="@+id/password" 37 android:layout_width="match_parent" 38 android:layout_height="wrap_content" 39 android:layout_weight="1" 40 android:hint="请输入"/> 41 </LinearLayout> 42 <Button 43 android:id="@+id/zc_bt" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:text="注册"/> 47 </LinearLayout>
Activity:
1 public class Main2Activity extends AppCompatActivity implements View.OnClickListener { 2 3 private EditText name; 4 private EditText password; 5 private Button zc_bt; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main2); 11 initView(); 12 } 13 14 private void initView() { 15 name = (EditText) findViewById(R.id.name); 16 password = (EditText) findViewById(R.id.password); 17 zc_bt = (Button) findViewById(R.id.zc_bt); 18 19 zc_bt.setOnClickListener(this); 20 } 21 22 @Override 23 public void onClick(View v) { 24 switch (v.getId()) { 25 case R.id.zc_bt: 26 String trim = name.getText().toString().trim(); 27 String trim1 = password.getText().toString().trim(); 28 //存储 29 SharedPreferences sp = getSharedPreferences("sp", 0); 30 SharedPreferences.Editor edit = sp.edit(); 31 edit.putString("name",trim); 32 edit.putString("password",trim1); 33 edit.commit(); 34 //跳转到登录页面 35 Intent intent = new Intent(this, MainActivity.class); 36 startActivity(intent); 37 break; 38 } 39 } 40 }