今天研究了一下Android的对话框,照着书上敲出来了一个简单的对话框。
DialogActivity.java:
1 package com.jin;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.Dialog;
6 import android.app.ProgressDialog;
7 import android.content.DialogInterface;
8 import android.os.Bundle;
9 import android.view.LayoutInflater;
10 import android.view.View;
11
12 public class DialogActivity extends Activity {
13
14 ProgressDialog m_Dialog;
15
16 @Override
17 public void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.main);
20
21 Dialog dialog = new AlertDialog.Builder(DialogActivity.this)
22 .setTitle("登陆提示")//设置标题
23 .setMessage("这里需要登录!")//设置内容
24 .setPositiveButton("确定",//设置确定按钮
25 new DialogInterface.OnClickListener(){
26 public void onClick(DialogInterface dialog, int whichButton){
27 //点击”确定“转向登陆框
28 LayoutInflater factory = LayoutInflater.from(DialogActivity.this);
29
30 //得到自定义对话框
31 final View DialogView = factory.inflate(R.layout.dialog, null);
32
33 //创建对话框
34 AlertDialog dlg = new AlertDialog.Builder(DialogActivity.this)
35 .setTitle("登陆框")
36 .setView(DialogView)//设置自定义对话框的样式
37 .setPositiveButton("确定",//设置”确定“按钮
38 new DialogInterface.OnClickListener(){//设置事件监听
39 public void onClick(DialogInterface dialog, int whichButton){
40 //输入完成后,点击”确定“开始登陆
41 m_Dialog = ProgressDialog.show(DialogActivity.this, "请等待...", "正在为你登陆...", true);
42
43 new Thread(){
44 public void run(){
45 try{
46 sleep(3000);
47 }catch(Exception e){
48 e.printStackTrace();
49 }finally{
50 //登陆结束,取消m_Dialog对话框
51 m_Dialog.dismiss();
52 }
53 }
54 }.start();
55 }
56 }).setNegativeButton("取消",//设置”取消“按钮
57 new DialogInterface.OnClickListener(){
58 public void onClick(DialogInterface dialog, int whichButton){
59 //点击”取消“按钮之后退出程序
60 DialogActivity.this.finish();
61 }
62 }).create();//创建
63 dlg.show();//显示
64 }
65
66 }).setNeutralButton("退出",
67 new DialogInterface.OnClickListener(){
68 public void onClick(DialogInterface dialog, int whichButton){
69 //点击”退出“按钮之后退出程序
70 DialogActivity.this.finish();
71 }
72 }).create();//创建按钮
73 dialog.show();
74 }
75 }
然后是dialog.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:id="@+id/username"
9 android:layout_height="wrap_content"
10 android:layout_width="wrap_content"
11 android:layout_marginLeft="20dip"
12 android:layout_marginRight="20dip"
13
14 android:text="@string/username"
15
16 android:gravity="left"
17 android:textAppearance="?android:attr/textAppearanceMedium" />
18
19 <EditText
20 android:id="@+id/un"
21 android:layout_width="fill_parent"
22 android:layout_height="wrap_content"
23 android:layout_marginLeft="20dip"
24 android:layout_marginRight="20dip"
25 android:scrollHorizontally="true"
26 android:autoText="false"
27 android:hint="@string/unhint"
28 android:inputType="text"
29 android:capitalize="none"
30 android:gravity="fill_horizontal"
31 android:textAppearance="?android:attr/textAppearanceMedium" />
32
33 <TextView
34 android:id="@+id/password"
35 android:layout_width="wrap_content"
36 android:layout_height="wrap_content"
37 android:layout_marginLeft="20dip"
38 android:layout_marginRight="20dip"
39
40 android:text="@string/password"
41 android:gravity="left"
42 android:textAppearance="?android:attr/textAppearanceMedium" />
43
44 <EditText
45 android:id="@+id/psw"
46 android:layout_width="fill_parent"
47 android:layout_height="wrap_content"
48 android:layout_marginLeft="20dip"
49 android:layout_marginRight="20dip"
50 android:scrollHorizontally="true"
51 android:autoText="false"
52 android:capitalize="none"
53 android:gravity="fill_horizontal"
54 android:hint="@string/pswhint"
55 android:inputType="textPassword"
56 android:password="true"
57 android:textAppearance="?android:attr/textAppearanceMedium" />
58
59 </LinearLayout>
运行效果:
在运行的过程中,一直报错,找不到原因,后来,终于发现,在dialog.xml中把EditText写成了EditView。粗心惹的祸啊,浪费了不少时间。
03-19 11:12:05.748: E/AndroidRuntime(368): android.view.InflateException: Binary XML file line #19: Error inflating class EditView