Android Dialog AlertDialog

Posted 语风6649

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Dialog AlertDialog相关的知识,希望对你有一定的参考价值。

1、普通的对话框

  

    

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:gravity="center"
 5     android:orientation="vertical" >
 6 
 7     <ProgressBar
 8         android:id="@+id/progressBar1"
 9         style="?android:attr/progressBarStyleLarge"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content" />
12 
13 </LinearLayout>
Dialog_progress.xml
 1 public class MyDialog extends Dialog{
 2 
 3     //必须要给构造方法
 4     public MyDialog(Context context) {
 5         //也可以在构建Dialog对象的时候就给指定Dialog样式
 6         //使用主题来修改Dialog样式,在res/values/styles.xml中添加自定义主题
 7         super(context,R.style.DialogTheme);
 8     }
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         //这个可以不要标题。通过getWindow().requestFeature(featureId)方法
14         //getWindow().requestFeature(Window.FEATURE_NO_TITLE);
15         setContentView(R.layout.dialog_progress);
16     }
17     
18     /**
19      * 一个Activity或者一个Dialog刚刚出现在用户面前的时候,焦点改变调用onWindowFocusChanged
20      */
21     @Override
22     public void onWindowFocusChanged(boolean hasFocus) {
23         super.onWindowFocusChanged(hasFocus);
24     }
25 }
MyDialog
 1 // 普通对话框
 2     public void dialog1(View v) {
 3         MyDialog dialog = new MyDialog(this);
 4         dialog.setTitle("这是进度Dialog");
 5         // 显示对话框
 6         dialog.show();
 7 
 8         // 关闭对话框用
 9         // dialog.dismiss();
10     }
普通对话框

 

2、警告对话框AlertDialog setMessage

 

 1 //AlertDialog  setMessage
 2     public void dialog2(View v) {
 3         AlertDialog.Builder dialog = new AlertDialog.Builder(this);
 4         dialog.setTitle("警告!").setIcon(R.drawable.ic_launcher)
 5                 .setMessage("前方高能")
 6                 // 注意这个导的包是import android.content.DialogInterface.OnClickListener;
 7                 .setPositiveButton("OK", new OnClickListener() {
 8                     @Override
 9                     public void onClick(DialogInterface dialog, int which) {
10                         Toast.makeText(MainActivity.this, "您选择了OK",
11                                 Toast.LENGTH_SHORT).show();
12 
13                     }
14                 })
15                 .setNegativeButton("Cancel", new OnClickListener() {
16                     @Override
17                     public void onClick(DialogInterface dialog, int which) {
18                         Toast.makeText(MainActivity.this, "您选择了Cancel",
19                                 Toast.LENGTH_SHORT).show();
20                     }
21                 })
22                 .setNeutralButton("Ignore", new OnClickListener() {
23                     @Override
24                     public void onClick(DialogInterface dialog, int which) {
25                         Toast.makeText(MainActivity.this, "您选择了Ignore",
26                                 Toast.LENGTH_SHORT).show();
27                     }
28                 })
29                 .create().show();
30     }
警告对话框,setMessage

 

 

3、菜单对话框AlertDialog setItem 

 1 //菜单选择,  setItem 如果设置setMessage,那么只会显示Message
 2     String[] setting = {"声音","存储","显示","应用","语言和输入法","流量使用情况","WLAN"};
 3     public void dialog3(View v){
 4         new AlertDialog.Builder(this)
 5         .setTitle("设置")
 6         .setIcon(R.drawable.setting)
 7         //which代表第几项,item点击后自动关闭,不需要Button
 8         .setItems(setting, new OnClickListener() {
 9             @Override
10             public void onClick(DialogInterface dialog, int which) {
11                 Toast.makeText(MainActivity.this, setting[which], Toast.LENGTH_SHORT).show();
12             }
13         })
14         .create().show();
15     }
菜单对话框 setItem

 

4、单选对话框AlertDialog setSingleChoiceItems

 1 //单选对话框,setItem
 2     String[] hobby = {"唱歌","跑步","武术","乒乓球","敲代码"};
 3     int choice = 0;
 4     public void dialog4(View v){
 5         new AlertDialog.Builder(this)
 6         .setTitle("爱好单选")
 7         .setIcon(R.drawable.hobby)
 8         //0代表默认选中第一个,选中不会自动关闭
 9         .setSingleChoiceItems(hobby, 0, new OnClickListener() {
10             @Override
11             public void onClick(DialogInterface dialog, int which) {
12                 choice = which;
13             }
14         })
15         //Button上的which永远为0,所以这里需要一个变量来保存选中的ItemID
16         .setPositiveButton("ok", new OnClickListener() {
17             @Override
18             public void onClick(DialogInterface dialog, int which) {
19                 Toast.makeText(MainActivity.this, hobby[choice], Toast.LENGTH_SHORT).show();
20             }
21         })
22         .setNegativeButton("cancel",null)
23         .create().show();
24     }
单选对话框 setSingleChoiceItems

 

5、多选对话框AlertDialog setMultiChoiceItem

 

 1 String[] hobby = {"唱歌","跑步","武术","乒乓球","敲代码"};
 2     boolean[] bool = {false,false,false,false,false};
 3     List<String> list = new ArrayList<String>();
 4     public void dialog5(View v){
 5         new AlertDialog.Builder(this)
 6         .setTitle("爱好可多选")
 7         .setIcon(R.drawable.hobby)
 8         //默认选中了哪些,点击也不会自动关闭
 9         .setMultiChoiceItems(hobby, bool, new OnMultiChoiceClickListener() {
10             @Override
11             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
12                 if(isChecked){
13                     list.add(hobby[which]);
14                 }else{
15                     list.remove(hobby[which]);
16                 }
17             }
18         })
19         .setPositiveButton("ok", new OnClickListener() {
20             @Override
21             public void onClick(DialogInterface dialog, int which) {
22                 
23                 Toast.makeText(MainActivity.this, list.toString(), Toast.LENGTH_SHORT).show();
24                 
25             }
26         }).create().show();
27     }
多选对话框AlertDialog setMultiChoiceItem

 

6、适配器对话框AlertDialog setAdapter

 

 1 public void dialog6(View v){
 2         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hobby);
 3         new AlertDialog.Builder(this)
 4         .setTitle("适配器对话框")
 5         //和setItem一样,选中之后对话框就自动消失,不需要Button
 6         .setAdapter(adapter, new OnClickListener() {
 7             @Override
 8             public void onClick(DialogInterface dialog, int which) {
 9                 Toast.makeText(MainActivity.this, hobby[which], Toast.LENGTH_SHORT).show();
10                 
11             }
12         }).create().show();
13     }
适配器对话框AlertDialog setAdapter

 

 7、自定义对话框AlertDialog setView

自定义对话框AlertDialog setView

 

8、关闭对话框AlertDialog dismisson

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical" >
 5     <ImageView
 6         android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:src="@drawable/ic_launcher"/>
 9     
10     <EditText
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:hint="关闭对话框请点击关闭按钮"/>
14     
15     <TextView
16         android:id="@+id/finish"
17         android:layout_width="wrap_content"
18            android:layout_height="wrap_content"
19            android:text="关闭"/>
20 
21 </LinearLayout>
Dialog_dismiss.xml
 1 public void dialog8(View v){
 2         View layout = getLayoutInflater().inflate(R.layout.dialog_dismiss, null);
 3         TextView finish = (TextView) layout.findViewById(R.id.finish);
 4         final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
 5         .setTitle("可关闭的对话框")
 6         .setView(layout)
 7         .create();
 8         dialog.show();
 9         finish.setOnClickListener(new View.OnClickListener() {
10             @Override
11             public void onClick(View v) {
12                 dialog.dismiss();
13             }
14         });
15         
16         
17         //有时候用户可能点到了外部,dialog就直接关闭了,而程序不知道,这时候就需要设置
18         dialog.setCancelable(false);
19         dialog.setOnDismissListener(new OnDismissListener() {
20             @Override
21             public void onDismiss(DialogInterface dialog) {
22                 Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_SHORT).show();
23                 
24             }
25         });
26     }
关闭对话框 AlertDialog setView

 

以上是关于Android Dialog AlertDialog的主要内容,如果未能解决你的问题,请参考以下文章

AlertDialog的解析

android怎样自定义dialog

android循环遍历出多个全屏的dialog,这些dialog怎么关闭

46.Android 自己定义Dialog

Android dialog能设置延迟或者倒计时吗

android怎样自定义dialog