1 package com.lidaochen.phonecall; 2 3 import android.content.Intent; 4 import android.content.SharedPreferences; 5 import android.net.Uri; 6 import android.os.Environment; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.text.TextUtils; 10 import android.util.Log; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.CheckBox; 14 import android.widget.EditText; 15 import android.widget.TabHost; 16 import android.widget.ThemedSpinnerAdapter; 17 import android.widget.Toast; 18 19 import java.util.Map; 20 21 public class MainActivity extends AppCompatActivity{ 22 private EditText et_userName; 23 private EditText et_pwd; 24 private CheckBox cb; 25 private SharedPreferences sp; 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 // 初始化sp实例 31 sp = getSharedPreferences("test", 0); 32 33 // 1.获取控件 34 et_userName = (EditText)findViewById(R.id.et_username); 35 et_pwd = (EditText)findViewById(R.id.et_pwd); 36 cb = (CheckBox)findViewById(R.id.cb); 37 // 把我们需要的数据在test.xml中显示出来,然后在控件上显示出来 38 String name = sp.getString("name", ""); 39 String pwd = sp.getString("pwd", ""); 40 // 将数据显示在控件上 41 et_userName.setText(name); 42 et_pwd.setText(pwd); 43 } 44 // 按钮点击事件 45 public void login(View v) 46 { 47 // 获取userName和pwd 48 String name = et_userName.getText().toString().trim(); 49 String pwd = et_pwd.getText().toString().trim(); 50 // 判断name和pwd是否为空 51 if(TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) 52 { 53 Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show(); 54 } 55 else 56 { 57 System.out.println("登录之后要干的事情!"); 58 if(cb.isChecked()) 59 { 60 // 获取sp的编辑器 61 SharedPreferences.Editor edit = sp.edit(); 62 edit.putString("name", name); 63 edit.putString("pwd", pwd); 64 // 把edit进行提交 65 edit.commit(); 66 Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_LONG).show(); 67 } 68 else 69 { 70 Toast.makeText(MainActivity.this, "请勾选复选框", Toast.LENGTH_LONG).show(); 71 } 72 } 73 } 74 }