1好久沒寫博客了,自從進入新公司以來學到了很多知識,博客是把自己學過的知識存儲并方便以后查看的好工具,最近一直在研究4.0源碼,以后更多的是深入研究源碼,對于應用可能寫的會很少,但是無論如何每天積累點,每天都學點,剛進公司老大讓寫一個記事本程序,要求能夠登錄,我在寫的時候加入了記住密碼和顯示密碼的功能,原本想加自動登錄功能,但是還是兩個選項比較合適,多了不多,直接上代碼。(今天時間較緊,寫的很倉促)package com.gionee.android.notepad.noteactivity; import com.gionee.android.notepad.service.FileService; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; import android.text.method.HideReturnsTransformationMethod; import android.text.method.PasswordTransformationMethod; import android.view.KeyEvent; 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; /** * @author Ma Guohui * @FileDescription:登陸Activity * @version 2012-10-26 下午2:55:03 * @ChangeList: */ public class Gn_LoginActivity extends Activity { private AutoCompleteTextView mUserNameAuto; private EditText mPasswordEt; private Button mLoginBt; private Button mCancelBt; private String mUserStr; private String mPwdStr; private CheckBox mRemPwdCb; private CheckBox mShowpwdCb; private SharedPreferences mPasswordSp; private FileService fileService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mUserNameAuto = (AutoCompleteTextView) findViewById(R.id.cardNumAuto); mPasswordEt = (EditText) findViewById(R.id.editPwd); mLoginBt = (Button) findViewById(R.id.button1); mCancelBt = (Button) findViewById(R.id.button2); mRemPwdCb = (CheckBox) findViewById(R.id.checkBox1); mShowpwdCb = (CheckBox) findViewById(R.id.checkBox2); fileService = new FileService(this); savePassword(); mLoginBt.setOnClickListener(new OnClickListener() {//登陸按鈕設置監(jiān)聽事件 public void onClick(View v) { // FileService fileService = new FileService(this); // TODO Auto-generated method stub // user = username.getText().toString(); mUserStr = mUserNameAuto.getText().toString().trim(); mPwdStr = mPasswordEt.getText().toString().trim(); if (fileService.login(mUserStr, mPwdStr)) {//執(zhí)行登錄驗證操作(fieService是另一個類的實例哦) if (mRemPwdCb.isChecked()) { //選擇記住密碼功能 mPasswordSp.edit().putString(mUserStr, mPwdStr).commit();//記住密碼,把密碼信息放入SharedPreferences文件中 } Intent intent = new Intent(Gn_LoginActivity.this, MainActivity.class); startActivity(intent); //跳轉到其他顯示界面 Gn_LoginActivity.this.finish(); } else { //提示密碼錯誤 Toast.makeText(Gn_LoginActivity.this, getResources().getString(R.string.password_error), Toast.LENGTH_SHORT).show(); } } }); mCancelBt.setOnClickListener(new OnClickListener() {//取消事件監(jiān)聽 @Override public void onClick(View v) { // TODO Auto-generated method stub dialog();//顯示對話框 } }); mShowpwdCb.setOnClickListener(new OnClickListener() {//顯示密碼事件操作 /* * 明文顯示密碼 : * 明文顯示:android.text.method.HideReturnsTransformationMethod ; * 密文顯示:android.text.method.PasswordTransformationMethod ; */ @Override public void onClick(View v) { // TODO Auto-generated method stub if (mShowpwdCb.isChecked()) {// 被選中,則顯示明文 // 將文本框的內(nèi)容設置成明文顯示 mPasswordEt.setTransformationMethod(HideReturnsTransformationMethod .getInstance()); } else { // 將文本框內(nèi)容設置成密文的方式顯示 mPasswordEt.setTransformationMethod(PasswordTransformationMethod .getInstance()); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {//監(jiān)聽返回鍵事件 // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { dialog(); } return false; } private void dialog() {//顯示對話框方法 (退出時哦) AlertDialog.Builder builder = new Builder(Gn_LoginActivity.this); builder.setMessage(this.getResources().getString(R.string.isLogout)); builder.setTitle(this.getResources().getString(R.string.logoutPrompt)); builder.setPositiveButton(this.getResources().getString(R.string.logout_yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); Gn_LoginActivity.this.finish(); } }); builder.setNegativeButton(this.getResources().getString(R.string.logout_no), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } private void savePassword() {//保存密碼方法,數(shù)據(jù)放入SharedPreferences文件 /* * 參數(shù)簡述: * Name—獲得SharedPreferences之后,將會在應用程序的私有文件夾中保存著一個XML文件,第一個參數(shù)name就是這個文件名字 * 。 Mode—XML文件的保存模式,默認為0,也就是MODE_PRIVATE */ mPasswordSp = this.getSharedPreferences("passwordFile", MODE_PRIVATE); mRemPwdCb.setChecked(true);// 默認為記住密碼 mUserNameAuto.setThreshold(1);// 輸入1個字母就開始自動提示 // 隱藏密碼為InputType.TYPE_TEXT_VARIATION_PASSWORD,也就是0x81 // 顯示密碼為InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,也就是0x91 mPasswordEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); mUserNameAuto.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub String[] allUserName = new String[mPasswordSp.getAll().size()];// sp.getAll().size()返回的是有多少個鍵值對 allUserName = mPasswordSp.getAll().keySet().toArray(new String[0]); ArrayAdapter<String> adapter = new ArrayAdapter<String>( Gn_LoginActivity.this, android.R.layout.simple_dropdown_item_1line, allUserName); mUserNameAuto.setAdapter(adapter);// 設置數(shù)據(jù)適配器 } @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 // 自動輸入密碼 mPasswordEt.setText(mPasswordSp.getString(mUserNameAuto.getText().toString(), "")); } }); } }
布局文件內(nèi)容
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/username" /> <AutoCompleteTextView android:id="@+id/cardNumAuto" android:layout_width="fill_parent" android:layout_height="wrap_content" > </AutoCompleteTextView> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/password" /> <EditText android:id="@+id/editPwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" > <requestFocus /> </EditText> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rempwd" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/showpwd" /> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陸" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout>
運行效果圖(沒圖說個JB)
|