android 如何实现登陆界面的记住密码功能
2013-05-29 17:03
801 查看
今天 写了一个有关登录记住密码的列子 其实这个例子的关键使用到了AutoCompleteTextView 以及sharedPreference的两个关键知识点,大家知道 AutoCompleteTextView 他的默认字体颜色以及背景颜色都是白色 所以我们要对下拉菜单的textview进行颜色定义区分好 我们先来看怎么区分的
list_item.xml
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:textSize="16sp" android:textColor="#000"> </TextView>
接下来是重点哦
/*author: conowen * date: 2012.4.2 * */ package com.hipikids.remeber; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class RemeberPwdActivity extends Activity { AutoCompleteTextView cardNumAuto; EditText passwordET; Button logBT; CheckBox savePasswordCB; SharedPreferences sp; String cardNumStr; String passwordStr; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_remeber_pwd); cardNumAuto = (AutoCompleteTextView) findViewById(R.id.cardNumAuto); passwordET = (EditText) findViewById(R.id.passwordET); logBT = (Button) findViewById(R.id.logBT); sp = this.getSharedPreferences("passwordFile", MODE_PRIVATE); savePasswordCB = (CheckBox) findViewById(R.id.savePasswordCB); savePasswordCB.setChecked(true);// 默认为记住密码 cardNumAuto.setThreshold(1);// 输入1个字母就开始自动提示 passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // 隐藏密码为InputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81 // 显示密码为InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91 cardNumAuto.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub String[] allUserName = new String[sp.getAll().size()];// sp.getAll().size()返回的是有多少个键值对 allUserName = sp.getAll().keySet().toArray(new String[0]); // sp.getAll()返回一张hash map // keySet()得到的是a set of the keys. // hash map是由key-value组成的 ArrayAdapter<String> adapter = new ArrayAdapter<String>( RemeberPwdActivity.this, R.layout.list_item, allUserName); cardNumAuto.setAdapter(adapter);// 设置数据适配器 } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub passwordET.setText(sp.getString(cardNumAuto.getText() .toString(), ""));// 自动输入密码 } }); // 登陆 logBT.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub cardNumStr = cardNumAuto.getText().toString(); passwordStr = passwordET.getText().toString(); if (!((cardNumStr.equals("test")) && (passwordStr .equals("test")))) { Toast.makeText(RemeberPwdActivity.this, "密码 误,请重新输入", Toast.LENGTH_SHORT).show(); } else { if (savePasswordCB.isChecked()) {// 登陆成功才保存密码 sp.edit().putString(cardNumStr, passwordStr).commit(); } Toast.makeText(RemeberPwdActivity.this, "登陆成功,正在获取用户数据……", Toast.LENGTH_SHORT).show(); // 跳转到另一个Activity // do something } } }); } }
activity_remeber_pwd.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/ic_launcher" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="简单登陆DEMO" android:textSize="25px" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="250dip" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="15dp" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:text="用 户 名:" android:textSize="15px" /> <AutoCompleteTextView android:id="@+id/cardNumAuto" android:textColor="#000000" android:layout_width="fill_parent" android:layout_height="wrap_content" > </AutoCompleteTextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:text="用户密码:" android:textSize="15px" /> <EditText android:id="@+id/passwordET" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/savePasswordCB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="记住密码" > </CheckBox> <Button android:id="@+id/logBT" android:layout_width="100px" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:layout_marginRight="10dp" android:text="登录" > </Button> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>
相关文章推荐
- Android实现带有记住密码功能的登陆界面
- Android自定义控件实现登陆界面以及SharedPreferences实现记住密码功能
- Android实现一个记住密码的登陆界面
- Android:用SharePreferences实现记住密码功能 (登陆案例)
- 实现一个记住密码的Android登陆界面
- Android探索之路:实现登录界面的记住密码功能
- SR 3-10444088391 : [ADF12.1.3]ADF如何实现登陆界面上的“记住我”功能
- Android实现一个记住密码的登陆界面
- Android实现一个记住密码的登陆界面
- Android学习笔记(19)————实现一个记住密码的Android登陆界面 .
- Android登录界面用SharedPreferences实现记住密码功能
- 说说在 Android 中如何实现记住密码功能
- Android通过SharedPreferences实现记住密码的登陆界面
- Android登录界面用SharedPreferences实现记住密码功能
- Android——SharedPreferences实现登录界面的记住密码和自动登录功能
- Android学习笔记(19)————实现一个记住密码的Android登陆界面
- android实现登陆注册记住密码功能
- Android——SharedPreferences实现登录界面的记住密码和自动登录功能
- Android实现一个记住密码的登陆界面
- Android登录界面用SharedPreferences实现记住密码功能