Android Studio学习路程(10)

【Android Studio学习路程(10)】学向勤中得,萤窗万卷书。这篇文章主要讲述Android Studio学习路程(10)相关的知识,希望能为你提供帮助。
今天学习了制作登录页面,并实现记住账户的功能。

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 { 15private EditText mEtPhone; 16private EditText mEtPasswd; 17private CheckBox mCBPsd; 18private String TAG ="MainActivity"; 19private String SP_PHONE = "sp_phone"; 20private String SP_PASSWD = "sp_passwd"; 21private String SP_IS_REMEMBER_PSD = "sp_is_remember_psd"; 22private SharedPreferences sharedPreferences; 23private boolean mIsChecked=false; 24 25@Override 26protected void onCreate(Bundle savedInstanceState) { 27super.onCreate(savedInstanceState); 28setContentView(R.layout.activity_main); 29//初始化控件 30initUI(); 31//初始化数据 32initData(); 33} 34 35private void initData() { 36//实例化sharedPreferences对象 37if(sharedPreferences==null){ 38sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 39} 40//回显数据 41mEtPhone.setText(sharedPreferences.getString(SP_PHONE,"")); 42mEtPasswd.setText(sharedPreferences.getString(SP_PASSWD,"")); 43mCBPsd.setChecked(mIsChecked); 44} 45 46private void initUI() { 47//获取电话和密码输入框 48mEtPhone = (EditText) findViewById(R.id.et_phone); 49//文本改变之后记录电话 50mEtPhone.addTextChangedListener(new TextWatcher() { 51@Override 52public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 53 54} 55 56@Override 57public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 58 59} 60 61@Override 62public void afterTextChanged(Editable editable) { 63if(mIsChecked){ 64if(sharedPreferences==null){ 65sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 66} 67SharedPreferences.Editor edit = sharedPreferences.edit(); 68edit.putString(SP_PHONE, mEtPhone.getText().toString()); 69edit.commit(); 70} 71} 72}); 73mEtPasswd = (EditText) findViewById(R.id.et_passwd); 74mEtPasswd.addTextChangedListener(new TextWatcher() { 75@Override 76public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 77 78} 79 80@Override 81public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 82 83} 84 85@Override 86public void afterTextChanged(Editable editable) { 87if(mIsChecked){ 88if(sharedPreferences==null){ 89sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 90} 91SharedPreferences.Editor edit = sharedPreferences.edit(); 92edit.putString(SP_PASSWD, mEtPasswd.getText().toString()); 93edit.commit(); 94} 95} 96}); 97//获取多选按钮 98mCBPsd = (CheckBox) findViewById( R.id.cb_remember_psd); 99mCBPsd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 100@Override 101public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 102Log.d(TAG, "状态为:" + isChecked); 103mIsChecked = isChecked; 104if (isChecked) { 105//实例化SharedPreferences对象 106if (sharedPreferences == null) { 107sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 108} 109//实例化SharedPreferences的编辑者对象 110SharedPreferences.Editor edit = sharedPreferences.edit(); 111edit.putString(SP_PHONE, mEtPhone.getText().toString()); 112edit.putString(SP_PASSWD, mEtPasswd.getText().toString()); 113edit.putBoolean(SP_IS_REMEMBER_PSD, isChecked); 114//提交 115edit.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" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:orientation="vertical" 7android:paddingBottom="@dimen/activity_vertical_margin" 8android:paddingLeft="@dimen/activity_horizontal_margin" 9android:paddingRight="@dimen/activity_horizontal_margin" 10android:paddingTop="@dimen/activity_vertical_margin" 11tools:context="com.example.hp.app4.MainActivity"> 12 13< EditText 14android:id="@+id/et_phone" 15android:inputType="phone" 16android:hint="电话:" 17android:layout_width="match_parent" 18android:layout_height="wrap_content" /> 19 20< EditText 21android:id="@+id/et_passwd" 22android:inputType="textPassword" 23android:hint="密码:" 24android:layout_width="match_parent" 25android:layout_height="wrap_content" /> 26< CheckBox 27android:id="@+id/cb_remember_psd" 28android:text="记住账户" 29android:layout_width="wrap_content" 30android:layout_height="wrap_content" /> 31 32< Button 33android:id="@+id/btn_denglu" 34android:text="登录" 35android:layout_width="match_parent" 36android:layout_height="wrap_content" /> 37 < /LinearLayout>

下面是运行的结果截图:
Android Studio学习路程(10)

文章图片

 
  然后虚拟机上返回,再打开:
 
Android Studio学习路程(10)

文章图片

 

    推荐阅读