`
butter
  • 浏览: 123021 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

分享一个Android中Dialog的使用例子

 
阅读更多

1、新建工程:DialogTest

2、编写布局文件:

(1)、main.xml   代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
    <Button
       android:id="@+id/button01"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/button01"
    />
     <Button
       android:id="@+id/button02"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/button02"
    />
     <Button
       android:id="@+id/button03"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/button03"
    />
     <Button
       android:id="@+id/button04"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/button04"
    />
</LinearLayout>
(2)alert_dialog_text_entry.xml   此部分布局是实现可以用户交互的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
     
    <TextView
      android:id="@+id/username_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="20dip"
      android:layout_marginRight="20dip"
      android:text="用户名"
      android:textAppearance="@android:style/TextAppearance.Medium"
    />
    <EditText
      android:id="@+id/username_edit"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="20dip"
      android:layout_marginRight="20dip"
      android:capitalize="none"
      android:textAppearance="@android:style/TextAppearance.Medium"
    />
       <TextView
      android:id="@+id/password_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="20dip"
      android:layout_marginRight="20dip"
      android:text="密码"
      android:textAppearance="@android:style/TextAppearance.Medium"
    />
     <EditText
      android:id="@+id/password_edit"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="20dip"
      android:layout_marginRight="20dip"
      android:capitalize="none"
      android:password="true"
      android:textAppearance="@android:style/TextAppearance.Medium"  
    />
</LinearLayout>
3、编写资源文件:strings.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">对话框</string>
    <string name="app_name">Android Dialog</string>
    <string name="button01">两个button的对话框</string>
    <string name="button02">三个button的对话框</string>
    <string name="button03">可以进行输入的对话框</string>
    <string name="button04">进度框</string>
    <string name="alert_dialog_two_buttons_title">这是一个提示框,点击取消后可以返回。</string>
    <string name="alert_dialog_ok">确定</string>
    <string name="alert_dialog_cancle">取消</string>
    <string name="alert_dialog_three_buttons_title">标题部分</string>
    <string name="alert_dialog_three_buttons_msg">对于程序员或创业团队来说,还是有必要拥有一个属于自己的博客。
    Wordpress 曾经让个人或企业搭建博客变得非常容易。但是我们觉得 Wordpress
       还是有些重量级,所以选择了一个非常轻便的工具 toto,一段只有200多行代码的Ruby应用程序。</string>
       <string name="alert_dialog_something">进入详细</string>
       <string name="alert_diaog_text_entry">请输入</string>
</resources>
4、开发主逻辑代码:DialogTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.dialogTest;
 
import com.listeview.R;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.method.CharacterPickerDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class DialogTest extends Activity {
    private static final int dialog4 = 4;
    private static final int dialog3 = 3;
    private static final int dialog2 = 2;
    private static final int dialog1 = 1;
    /** Called when the activity is first created. */
    private Button button01;
    private Button button02;
    private Button button03;
    private Button button04;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        button01=(Button)findViewById(R.id.button01);
        button02=(Button)findViewById(R.id.button02);
        button03=(Button)findViewById(R.id.button03);
        button04=(Button)findViewById(R.id.button04);
         
        button01.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(dialog1);
            }
        });
         
        button02.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(dialog2);
            }
        });
         
        button03.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(dialog3);
            }
        });
         
        button04.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(dialog4);
            }
        });
         
    }
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch(id){
        case dialog1:
            return buildDialog1(DialogTest.this);
        case dialog2:
            return buildDialog2(DialogTest.this);
        case dialog3:
            return buildDialog3(DialogTest.this);
        case dialog4:
            return buildDialog4(DialogTest.this);
        }
        return null;
    }
    private Dialog buildDialog4(Context context) {
        ProgressDialog dialog=new ProgressDialog(context);
        dialog.setTitle("正在下载歌曲");
        dialog.setMessage("请稍候......");
         
        /*TimePickerDialog dialog=new TimePickerDialog(context, 0, null, 0, 0, false);
        dialog.setTitle("时钟");*/
         
        /*DatePickerDialog dialog=new DatePickerDialog(context, 0, null, 0, 0, 0);
        dialog.setTitle("日期");*/
         
        return dialog;
    }
    private Dialog buildDialog3(Context context) {
        LayoutInflater inflater=LayoutInflater.from(this);
        final View textEntryView=inflater.inflate(R.layout.alert_dialog_text_entry, null);
         
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle(R.string.alert_diaog_text_entry);
         
        builder.setView(textEntryView); //关键
        builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
             
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                setTitle("单击对话框上的确定按钮");
            }
        });
        builder.setNegativeButton(R.string.alert_dialog_cancle,new DialogInterface.OnClickListener() {
             
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                setTitle("单击了对话框上的取消按钮");
            }
        });
        return builder.create();
    }
     
     
    private Dialog buildDialog2(Context context) {
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle(R.string.alert_dialog_three_buttons_title);
        builder.setMessage(R.string.alert_dialog_three_buttons_msg);
        builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                setTitle("单击对话框上的确定按钮");
            }
        });
        builder.setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                setTitle("点击了对话框上的进入详细按钮");
            }
        });
        builder.setNegativeButton(R.string.alert_dialog_cancle, new DialogInterface.OnClickListener() {
             
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                setTitle("单击了对话框上的取消按钮");
            }
        });
        return builder.create();
    }
     
    private Dialog buildDialog1(Context context) {
        // TODO Auto-generated method stub
        AlertDialog.Builder builder=new AlertDialog.Builder(context);
        //builder.setIcon(R.drawable.icon);
        builder.setTitle(R.string.alert_dialog_two_buttons_title);
        builder.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                setTitle("单击对话框上的确定按钮");
            }
        });
        builder.setNegativeButton(R.string.alert_dialog_cancle, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                setTitle("单击了对话框上的取消按钮");
            }
        });
        return builder.create();
    }
}
5、运行效果显示:

 

主程序:

Dialog1:

Dialog2:

Dialog3:

Dialog4:


<!--EndFragment-->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics