1好久沒(méi)寫(xiě)博客了,自從進(jìn)入新公司以來(lái)學(xué)到了很多知識(shí),博客是把自己學(xué)過(guò)的知識(shí)存儲(chǔ)并方便以后查看的好工具,最近一直在研究4.0源碼,以后更多的是深入研究源碼,對(duì)于應(yīng)用可能寫(xiě)的會(huì)很少,但是無(wú)論如何每天積累點(diǎn),每天都學(xué)點(diǎn),剛進(jìn)公司老大讓寫(xiě)一個(gè)記事本程序,要求能夠登錄,我在寫(xiě)的時(shí)候加入了記住密碼和顯示密碼的功能,原本想加自動(dòng)登錄功能,但是還是兩個(gè)選項(xiàng)比較合適,多了不多,直接上代碼。(今天時(shí)間較緊,寫(xiě)的很倉(cāng)促)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() {//登陸按鈕設(shè)置監(jiān)聽(tīng)事件 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í)行登錄驗(yàn)證操作(fieService是另一個(gè)類(lèi)的實(shí)例哦) if (mRemPwdCb.isChecked()) { //選擇記住密碼功能 mPasswordSp.edit().putString(mUserStr, mPwdStr).commit();//記住密碼,把密碼信息放入SharedPreferences文件中 } Intent intent = new Intent(Gn_LoginActivity.this, MainActivity.class); startActivity(intent); //跳轉(zhuǎn)到其他顯示界面 Gn_LoginActivity.this.finish(); } else { //提示密碼錯(cuò)誤 Toast.makeText(Gn_LoginActivity.this, getResources().getString(R.string.password_error), Toast.LENGTH_SHORT).show(); } } }); mCancelBt.setOnClickListener(new OnClickListener() {//取消事件監(jiān)聽(tīng) @Override public void onClick(View v) { // TODO Auto-generated method stub dialog();//顯示對(duì)話(huà)框 } }); 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)容設(shè)置成明文顯示 mPasswordEt.setTransformationMethod(HideReturnsTransformationMethod .getInstance()); } else { // 將文本框內(nèi)容設(shè)置成密文的方式顯示 mPasswordEt.setTransformationMethod(PasswordTransformationMethod .getInstance()); } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {//監(jiān)聽(tīng)返回鍵事件 // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { dialog(); } return false; } private void dialog() {//顯示對(duì)話(huà)框方法 (退出時(shí)哦) 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ù)簡(jiǎn)述: * Name—獲得SharedPreferences之后,將會(huì)在應(yīng)用程序的私有文件夾中保存著一個(gè)XML文件,第一個(gè)參數(shù)name就是這個(gè)文件名字 * 。 Mode—XML文件的保存模式,默認(rèn)為0,也就是MODE_PRIVATE */ mPasswordSp = this.getSharedPreferences("passwordFile", MODE_PRIVATE); mRemPwdCb.setChecked(true);// 默認(rèn)為記住密碼 mUserNameAuto.setThreshold(1);// 輸入1個(gè)字母就開(kāi)始自動(dòng)提示 // 隱藏密碼為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()返回的是有多少個(gè)鍵值對(duì) 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è)置數(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 // 自動(dòng)輸入密碼 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>
運(yùn)行效果圖(沒(méi)圖說(shuō)個(gè)JB)
|