Android Studio学习路程(10)
Posted mashuxia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 { 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=false; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 //初始化控件 30 initUI(); 31 //初始化数据 32 initData(); 33 } 34 35 private void initData() { 36 //实例化sharedPreferences对象 37 if(sharedPreferences==null){ 38 sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 39 } 40 //回显数据 41 mEtPhone.setText(sharedPreferences.getString(SP_PHONE,"")); 42 mEtPasswd.setText(sharedPreferences.getString(SP_PASSWD,"")); 43 mCBPsd.setChecked(mIsChecked); 44 } 45 46 private void initUI() { 47 //获取电话和密码输入框 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, "状态为:" + isChecked); 103 mIsChecked = isChecked; 104 if (isChecked) { 105 //实例化SharedPreferences对象 106 if (sharedPreferences == null) { 107 sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE); 108 } 109 //实例化SharedPreferences的编辑者对象 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>
下面是运行的结果截图:
然后虚拟机上返回,再打开:
以上是关于Android Studio学习路程(10)的主要内容,如果未能解决你的问题,请参考以下文章