Android Studio distance learning (10)

Today learned to make the login page, and remember to achieve functional account.

  1 package com.example.hp.app4;
  2 
  3 import android.content.Context;
  4 import android.content.SharedPreferences;
  5 import android.os.Bundle;
  6 import android.support.v7.app.ActionBarActivity;
  7 import android.text.Editable;
  8 import android.text.TextWatcher;
  9 import android.util.Log;
 10 import android.widget.CheckBox;
 11 import android.widget.CompoundButton;
 12 import android.widget.EditText;
 13 
 14 public class MainActivity extends ActionBarActivity {
 15     private EditText mEtPhone;
 16     private EditText mEtPasswd;
 17     private CheckBox mCBPsd;
 18     private String TAG ="MainActivity";
 19     private String SP_PHONE = "sp_phone";
 20     private String SP_PASSWD = "sp_passwd";
 21     private String SP_IS_REMEMBER_PSD = "sp_is_remember_psd";
 22     private SharedPreferences sharedPreferences;
 23     Private  Boolean mIsChecked = to false ;
 24  
25      @Override
 26 is      protected  void the onCreate (the Bundle savedInstanceState) {
 27          Super .onCreate (savedInstanceState);
 28          the setContentView (R.layout.activity_main);
 29          // initialize control 
30          initUI ();
 31 is          // initialized data 
32          initData ();
 33 is      }
 34 is  
35      Private  void initData () {
 36          // instantiate objects sharedPreferences 
37 [          IF(SharedPreferences == null ) {
 38 is              SharedPreferences = getApplicationContext () getSharedPreferences ( "config." , Context.MODE_PRIVATE);
 39          }
 40          // echo data 
41 is          mEtPhone.setText (sharedPreferences.getString (SP_PHONE, "" ));
 42 is          mEtPasswd.setText (sharedPreferences.getString (SP_PASSWD, "" ));
 43 is          mCBPsd.setChecked (mIsChecked);
 44 is      }
 45  
46 is      Private  void initUI () {
 47          // Get telephone and password input box 
48          mEtPhone = (EditText) findViewById(R.id.et_phone);
 49         //文本改变之后记录电话
 50         mEtPhone.addTextChangedListener(new TextWatcher() {
 51             @Override
 52             public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 53 
 54             }
 55 
 56             @Override
 57             public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 58 
 59             }
 60 
 61             @Override
 62             public void afterTextChanged(Editable editable) {
 63                 if(mIsChecked){
 64                     if(sharedPreferences==null){
 65                         sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
 66                     }
 67                     SharedPreferences.Editor edit = sharedPreferences.edit();
 68                     edit.putString(SP_PHONE, mEtPhone.getText().toString());
 69                     edit.commit();
 70                 }
 71             }
 72         });
 73         mEtPasswd = (EditText) findViewById(R.id.et_passwd);
 74         mEtPasswd.addTextChangedListener(new TextWatcher() {
 75             @Override
 76             public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 77 
 78             }
 79 
 80             @Override
 81             public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 82 
 83             }
 84 
 85             @Override
 86             public void afterTextChanged(Editable editable) {
 87                 if(mIsChecked){
 88                     if(sharedPreferences==null){
 89                         sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
 90                     }
 91                     SharedPreferences.Editor edit = sharedPreferences.edit();
 92                     edit.putString(SP_PASSWD, mEtPasswd.getText().toString());
 93                     edit.commit();
 94                 }
 95             }
 96         });
 97         //获取多选按钮
 98         mCBPsd = (CheckBox) findViewById( R.id.cb_remember_psd);
 99         mCBPsd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
100             @Override
101             public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
102                 Log.d (TAG, "state:" + the isChecked);
 103                  mIsChecked = the isChecked;
 104                  IF (the isChecked) {
 105                      // instantiate objects SharedPreferences 
106                      IF (SharedPreferences == null ) {
 107                          . SharedPreferences = getApplicationContext () getSharedPreferences ( "config" , Context.MODE_PRIVATE);
 108                      }
 109                      // instantiate objects SharedPreferences editors 
110                      SharedPreferences.Editor edit = sharedPreferences.edit ();
 111                     edit.putString(SP_PHONE, mEtPhone.getText().toString());
112                     edit.putString(SP_PASSWD, mEtPasswd.getText().toString());
113                     edit.putBoolean(SP_IS_REMEMBER_PSD, isChecked);
114                     //提交
115                     edit.commit();
116 
117 
118                 }
119             }
120         });
121     }
122 }
 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:orientation="vertical"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="com.example.hp.app4.MainActivity">
12 
13     <EditText
14         android:id="@+id/et_phone"
15         android:inputType="phone"
16         android:hint="电话:"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content" />
19 
20     <EditText
21         android:id="@+id/et_passwd"
22         android:inputType="textPassword"
23         android:hint="密码:"
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content" />
26     <CheckBox
27         android:id="@+id/cb_remember_psd"
28         android:text="记住账户"
29         android:layout_width="wrap_content"
30         android:layout_height="wrap_content" />
31 
32     <Button
33         android:id="@+id/btn_denglu"
34         android:text="登录"
35         android:layout_width="match_parent"
36         android:layout_height="wrap_content" />
37 </LinearLayout>

The following is a screenshot of the result of running:

 

 Back then the virtual machine, and then open:

 

 

Guess you like

Origin www.cnblogs.com/mxk123456/p/12315346.html