android自定义登陆Dialog

Preview

Question

我们需要自定义一个登陆界面,当我们密码输入成功的时候进入下一个页面,密码不匹配的时候将输入的内容清空。

Solution

001 package com.study;
002
003 import java.lang.reflect.Field;
004
005 import android.app.Activity;
006 import android.app.AlertDialog;
007 import android.app.ProgressDialog;
008 import android.content.DialogInterface;
009 import android.content.Intent;
010 import android.database.Cursor;
011 import android.database.sqlite.SQLiteDatabase;
012 import android.os.Bundle;
013 import android.os.Handler;
014 import android.view.LayoutInflater;
015 import android.view.View;
016 import android.widget.EditText;
017
018 public class Login extends Activity {
019     /** Called when the activity is first created. */
020
021     private DBHelpter dbHelpter;
022     private SQLiteDatabase db;
023     private String name;
024     private String password;
025     private EditText editTextName;
026     private EditText editTextPassword;
027     private Cursor cursor;
028     private ProgressDialog progressDialog;
029
030     @Override
031     public void onCreate(Bundle savedInstanceState) {
032         super.onCreate(savedInstanceState);
033         setContentView(R.layout.main);
034         Handler handler = new Handler();
035         handler.post(runableCreateDialog);
036     }
037
038     Runnable runableCreateDialog = new Runnable() {
039
040         @Override
041         public void run() {
042             // TODO Auto-generated method stub
043             // 因为是动态加载xml布局文件,要得到xml里面的控件必须这样做
044             LayoutInflater factory = LayoutInflater.from(Login.this);
045             final View view = factory.inflate(R.layout.login, null);
046
047             // 生成一个自定义的AlertDialog
048             new AlertDialog.Builder(Login.this).setTitle(
049                     getResources().getString(R.string.dialogTitle)).setView(
050                     view).setNegativeButton(
051                     getResources().getString(R.string.register),
052                     new DialogInterface.OnClickListener() {
053
054                         @Override
055                         public void onClick(DialogInterface dialog, int which) {
056                             // TODO Auto-generated method stub
057                             try {
058                                 Field field = dialog.getClass().getSuperclass()
059                                         .getDeclaredField("mShowing");
060                                 field.setAccessible(true);
061                                 field.set(dialog, true);
062                                 dialog.dismiss();
063
064                             } catch (Exception e) {
065
066                             }
067                             Intent intent = new Intent(Login.this,
068                                     Register.class);
069                             startActivity(intent);
070
071                         }
072                     }).setPositiveButton(
073                     getResources().getString(R.string.confirm),
074                     new DialogInterface.OnClickListener() {
075
076                         @Override
077                         public void onClick(DialogInterface dialog, int which) {
078                             // TODO Auto-generated method stub
079                             try {
080                                 Field field = dialog.getClass().getSuperclass()
081                                         .getDeclaredField("mShowing");
082                                 field.setAccessible(true);
083
084                                 dbHelpter = new DBHelpter(Login.this);
085                                 db = dbHelpter.getReadableDatabase();
086                                 editTextName = (EditText) view
087                                         .findViewById(R.id.editTextUserName);
088                                 editTextPassword = (EditText) view
089                                         .findViewById(R.id.editTextPassword);
090                                 name = editTextName.getText().toString();
091                                 password = editTextPassword.getText()
092                                         .toString();
093
094                                 try {
095                                     cursor = db.rawQuery(
096                                             "select * from tbluser where "
097                                                     + UserColumns.userName
098                                                     + " = ? and "
099                                                     + UserColumns.userPassword
100                                                     + " = ?", new String[] {
101                                                     name, password });
102                                     if (cursor.getCount() > 0) {
103                                         progressDialog = ProgressDialog
104                                                 .show(
105                                                         Login.this,
106                                                         getResources()
107                                                                 .getString(
108                                                                         R.string.progressTitle),
109                                                         getResources()
110                                                                 .getString(
111                                                                         R.string.loginNow),
112                                                         true);
113
114                                         new Thread() {
115                                             @Override
116                                             public void run() {
117                                                 // TODO Auto-generated method
118                                                 // stub
119                                                 try {
120                                                     sleep(3000);
121                                                 } catch (Exception ex) {
122                                                     ex.printStackTrace();
123                                                 } finally {
124                                                     if (progressDialog != null)
125                                                         progressDialog
126                                                                 .dismiss();
127                                                 }
128                                             }
129
130                                         }.start();
131                                         System.out.println("登陆成功");
132                                         field.set(dialog, true);
133                                         dialog.dismiss();
134                                         Intent intent = new Intent(Login.this,
135                                                 Information.class);
136                                         startActivity(intent);
137                                     } else {
138                                         editTextName.setText("");
139                                         editTextPassword.setText("");
140                                         field.set(dialog, false);
141                                         dialog.dismiss();
142                                         System.out.println("登陆失败");
143                                     }
144                                 } finally {
145                                     try {
146                                         if (cursor != null) {
147                                             if (!cursor.isClosed())
148                                                 cursor.close();
149                                         }
150                                     } finally {
151                                         try {
152                                             if (db != null) {
153                                                 db.close();
154                                             }
155                                         } finally {
156                                             if (dbHelpter != null) {
157                                                 dbHelpter.close();
158                                             }
159                                         }
160                                     }
161                                 }
162
163                             } catch (Exception e) {
164
165                             }
166                         }
167                     }).show();
168         }
169     };
170
171     @Override
172     protected void onStop() {
173         // TODO Auto-generated method stub
174         this.finish();
175         super.onStop();
176     }
177 }
posted @ 2011-09-30 15:14  havemydream  阅读(2874)  评论(4编辑  收藏  举报