android 各种常用,不易记的dialog,对话框,等控件整理
博客专区 > SuShine 的博客 > 博客详情
android 各种常用,不易记的dialog,对话框,等控件整理
SuShine 发表于2年前
android 各种常用,不易记的dialog,对话框,等控件整理
  • 发表于 2年前
  • 阅读 19
  • 收藏 0
  • 点赞 0
  • 评论 0

在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助。

1.该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式。

创建dialog对话框方法代码如下:

protected void dialog() {
    AlertDialog.Builder builder = new Builder(Main.this);
    builder.setMessage("确认退出吗?");
    builder.setTitle("提示");
    builder.setPositiveButton("确认", new OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
      Main.this.finish();
     }
    });
    builder.setNegativeButton("取消", new OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
     }
    });
    builder.create().show();
   }
在onKeyDown(int keyCode, KeyEvent event)方法中调用此方法 
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
     dialog();
    }
    return false;
   }
2.改变了对话框的图表,添加了三个按钮

创建dialog的方法代码如下:

Dialog dialog = new AlertDialog.Builder(this).setIcon(
       android.R.drawable.btn_star).setTitle("喜好调查").setMessage(
       "你喜欢李连杰的电影吗?").setPositiveButton("很喜欢",
       new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
         // TODO Auto-generated method stub
         Toast.makeText(Main.this, "我很喜欢他的电影。",
           Toast.LENGTH_LONG).show();
        }
       }).setNegativeButton("不喜欢", new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       Toast.makeText(Main.this, "我不喜欢他的电影。", Toast.LENGTH_LONG)
         .show();
      }
     }).setNeutralButton("一般", new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       Toast.makeText(Main.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG)
         .show();
      }
     }).create();
     dialog.show();
3.信息内容是一个简单的View类型

创建dialog方法的代码如下:

new AlertDialog.Builder(this).setTitle("请输入").setIcon(
       android.R.drawable.ic_dialog_info).setView(
       new EditText(this)).setPositiveButton("确定", null)
       .setNegativeButton("取消", null).show();
4.信息内容是一组单选框

创建dialog方法的代码如下:

new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems(
       new String[] { "Item1", "Item2" }, null, null)
       .setPositiveButton("确定", null)
       .setNegativeButton("取消", null).show();
5.信息内容是一组多选框

创建dialog方法的代码如下:

new AlertDialog.Builder(this).setTitle("单选框").setIcon(
       android.R.drawable.ic_dialog_info).setSingleChoiceItems(
       new String[] { "Item1", "Item2" }, 0,
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
         dialog.dismiss();
        }
       }).setNegativeButton("取消", null).show();
6.信息内容是一组简单列表项

创建dialog的方法代码如下:

new AlertDialog.Builder(this).setTitle("列表框").setItems(
       new String[] { "Item1", "Item2" }, null).setNegativeButton(
       "确定", null).show();
7.信息内容是一个自定义的布局

dialog布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content" android:layout_width="wrap_content"
   android:background="#ffffffff" android:orientation="horizontal"
   android:id="@+id/dialog">
   <TextView android:layout_height="wrap_content"
     android:layout_width="wrap_content"
    android:id="@+id/tvname" android:text="姓名:" />
   <EditText android:layout_height="wrap_content"
    android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>
  </LinearLayout>
创建dialog方法的代码如下: 
LayoutInflater inflater = getLayoutInflater();
     View layout = inflater.inflate(R.layout.dialog,
       (ViewGroup) findViewById(R.id.dialog));
     new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
       .setPositiveButton("确定", null)
       .setNegativeButton("取消", null).show();

好了,以上7种Android dialog对话框的使用方法就介绍到这里了,基本都全了,如果大家在android开发过程中遇到dialog的时候就可以拿出来看看。

package com.su.testalldialog;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

public class TestAllDialogActivity extends Activity {
	String[] weekdays;
	Context context;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		context = this;
		weekdays = new String[] { "Sun", "Mon", "Tue", "Wed" };
		Button button1 = (Button) findViewById(R.id.button1);
		Button button2 = (Button) findViewById(R.id.button2);
		Button button3 = (Button) findViewById(R.id.button3);
		Button button4 = (Button) findViewById(R.id.button4);
		Button button5 = (Button) findViewById(R.id.button5);
		Button button6 = (Button) findViewById(R.id.button6);
		button6.setOnClickListener(listener);
		button5.setOnClickListener(listener);
		button1.setOnClickListener(listener);
		button2.setOnClickListener(listener);
		button3.setOnClickListener(listener);
		button4.setOnClickListener(listener);
		initSpinner();
		initAutocompletetextview();

	}

	private void initAutocompletetextview() {
		AutoCompleteTextView autocompletetextview = (AutoCompleteTextView) findViewById(R.id.autocompletetextview);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_dropdown_item, weekdays);// 实现简单的adapter
		autocompletetextview.setAdapter(adapter);
	}

	private void initSpinner() {
		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		spinner.setOnItemSelectedListener(slistener);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, weekdays);// 实现简单的adapter
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 实现选项的样式
		spinner.setAdapter(adapter);
	}

	private OnItemSelectedListener slistener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			toast(weekdays[arg2]);

		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}
	};

	int index = 0;// 用来标记哪一项被选择
	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			Builder dialog = new AlertDialog.Builder(context);
			switch (v.getId()) {

			case R.id.button1:
				dialog.setIcon(R.drawable.ic_launcher);
				dialog.setTitle("简单选择对话框");
				dialog.setItems(weekdays,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								toast(weekdays[which]);

							}
						});
				dialog.show();
				break;
			case R.id.button2:
				dialog.setIcon(R.drawable.ic_launcher);
				dialog.setTitle("单选对话框");
				dialog.setSingleChoiceItems(weekdays, 1,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								index = which;

							}
						});
				dialog.setPositiveButton("是",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								toast(weekdays[index]);
							}
						});
				dialog.setNegativeButton("否",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {

							}
						});

				dialog.show();
				break;
			case R.id.button3:
				final ArrayList<String> multiWeekadys = new ArrayList<String>();// 选择的数据放到list中

				dialog.setIcon(R.drawable.ic_launcher);
				dialog.setTitle("复选对话框");
				final boolean[] checkeditems = new boolean[weekdays.length];// 设置一打开时的选择情况
				dialog.setMultiChoiceItems(weekdays, checkeditems,
						new DialogInterface.OnMultiChoiceClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which, boolean isChecked) {
								if (isChecked) {
									checkeditems[which] = true;// 获取一个被选中的列表

								}

							}
						});

				dialog.setPositiveButton("是",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								for (int j = 0; j < checkeditems.length; j++) {// 遍历一遍看那个是选中的
									if (checkeditems[j]) {
										multiWeekadys.add(weekdays[j]);
									}

								}

								toast(multiWeekadys.toString());
							}
						});
				dialog.setNegativeButton("否",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {

							}
						});
				dialog.show();
				break;
			case R.id.button4:
				dialog.setIcon(R.drawable.ic_launcher);
				dialog.setTitle("简单警告");
				dialog.setMessage("测试警告");
				dialog.setPositiveButton("是",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								toast("是");

							}
						});
				dialog.setNeutralButton("中",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								toast("中");
							}
						});
				dialog.setNegativeButton("否",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {

							}
						});

				dialog.show();
				break;
			case R.id.button5:
				final EditText et = new EditText(context);
				et.setText("测试");
				dialog.setIcon(R.drawable.ic_launcher);
				dialog.setTitle("简单警告");
				dialog.setMessage("测试警告");
				dialog.setView(et);
				dialog.setPositiveButton("是",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								toast(et.getText().toString());

							}
						});
				dialog.setNeutralButton("中",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								toast("中");
							}
						});
				dialog.setNegativeButton("否",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {

							}
						});

				dialog.show();
				break;
			case R.id.button6:

				final ImageView im = new ImageView(context);
				im.setImageResource(R.drawable.ic_launcher);

				dialog.setIcon(R.drawable.ic_launcher);
				dialog.setTitle("简单警告");
				dialog.setMessage("测试警告");
				dialog.setView(im);

				dialog.setPositiveButton("是",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {

							}
						});
				dialog.setNeutralButton("中",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								toast("中");
							}
						});
				dialog.setNegativeButton("否",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {

							}
						});

				dialog.show();
				break;

			default:
				break;
			}

		}
	};

	private void toast(String msg) {
		Toast.makeText(context, msg, 10).show();

	}
}

package safemobile.mobi;

import java.util.ArrayList;

import com.casee.adsdk.CaseeAdView;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class test extends Activity implements Runnable {
    /** Called when the activity is first created. */
	  
    /**确定取消信息框 **/
    private static final int DIALOG_0 = 1;
    /**多个按钮信息框 **/
    private static final int DIALOG_1 = 2;
    /**列表框 **/
    private static final int DIALOG_2 = 3;
    /**进度条框 **/
    private static final int DIALOG_3 = 4;
    /**单项选择列表框 **/
    private static final int DIALOG_4 = 5;
    /**多项选择列表框 **/
    private static final int DIALOG_5 = 6;
    /**自定义布局 **/
    private static final int DIALOG_6 = 7;
    /**读取进度框 **/
    private static final int DIALOG_7 = 8;
    
    private static final int MAX_PROGRESS = 100;
    
    private ProgressDialog mProgressDialog  = null;
    
    final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};
    
    int mSingleChoiceID = -1;
    
    ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();
    
	CaseeAdView cav;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cav = (CaseeAdView) this.findViewById(R.id.caseeAdView);
        Button button0 = (Button) findViewById(R.id.button0);
        button0.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        	CreatDialog(DIALOG_0);
            }
        });
        
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        	CreatDialog(DIALOG_1);
            }
        });
        
        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        	CreatDialog(DIALOG_2);
            }
        });
    
        Button button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        	CreatDialog(DIALOG_3);
        	mProgressDialog.setProgress(0);
            }
        });
     
        Button button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        	CreatDialog(DIALOG_4);
            }
        });  
        
        Button button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        	CreatDialog(DIALOG_5);
            }
        }); 
    
        Button button6 = (Button) findViewById(R.id.button6);
        button6.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        	CreatDialog(DIALOG_6);
            }
        }); 
        
        Button button7 = (Button) findViewById(R.id.button7);
        button7.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        	CreatDialog(DIALOG_7);
            }
        }); 
    }

    public void CreatDialog(int id) {
	AlertDialog.Builder builder = new AlertDialog.Builder(test.this);	
	switch(id) {
	case DIALOG_0:
	    builder.setIcon(R.drawable.icon);
            builder.setTitle("你确定要离开吗?");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //这里添加点击确定后的逻辑
                    showDialog("你选择了确定");
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //这里添加点击确定后的逻辑
                    showDialog("你选择了取消");
                }
            });
	    break;
	case DIALOG_1:
	    builder.setIcon(R.drawable.icon);
            builder.setTitle("投票");
            builder.setMessage("您认为什么样的内容能吸引您?");
            builder.setPositiveButton("有趣味的", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    showDialog("你选择了有趣味的");
                }
            });
            builder.setNeutralButton("有思想的", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    showDialog("你选择了有思想的");                    
                }
            });
            builder.setNegativeButton("主题强的", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    showDialog("你选择了主题强的");  
                }
            });
	    break;
	case DIALOG_2:
	    builder.setTitle("列表选择框");
            builder.setItems(mItems, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //点击后弹出窗口选择了第几项
                    showDialog("你选择的id为" + which + " , " + mItems[which]);
                }
            });
	    break;
	case DIALOG_3:
	            mProgressDialog = new ProgressDialog(test.this);
	            mProgressDialog.setIcon(R.drawable.icon);
	            mProgressDialog.setTitle("进度条窗口");
	            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	            mProgressDialog.setMax(MAX_PROGRESS);
	            mProgressDialog.setButton("确定", new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int whichButton) {
	                    //这里添加点击后的逻辑
	                }
	            });
	            mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
	                public void onClick(DialogInterface dialog, int whichButton) {
	                    //这里添加点击后的逻辑
	                }
	            });
	            mProgressDialog.show();
	            new Thread(this).start();
	     return;
	case DIALOG_4:
	     mSingleChoiceID = -1;
	     builder.setIcon(R.drawable.icon);
             builder.setTitle("单项选择");
             builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int whichButton) {
                         mSingleChoiceID = whichButton;
                         showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
                 }
             });
             builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int whichButton) {
                     if(mSingleChoiceID > 0) {
                	 showDialog("你选择的是" + mSingleChoiceID);
                     }
                 }
             });
             builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int whichButton) {

                 }
             });
	    break;
	case DIALOG_5:
	    MultiChoiceID.clear();
	    builder.setIcon(R.drawable.icon);
            builder.setTitle("多项选择");
            builder.setMultiChoiceItems(mItems,
                    new boolean[]{false, false, false, false, false, false, false},
                    new DialogInterface.OnMultiChoiceClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton,
                                boolean isChecked) {
                           if(isChecked) {
                               MultiChoiceID.add(whichButton);
                               showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
                           }else {
                               MultiChoiceID.remove(whichButton);
                           }
                            
                        }
                    });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String str = "";
                    int size = MultiChoiceID.size();
                    for (int i = 0 ;i < size; i++) {
                	str+= mItems[MultiChoiceID.get(i)] + ", ";
                    }
                    showDialog("你选择的是" + str);
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });
	    break;
	case DIALOG_6:
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.test, null);
            	builder.setIcon(R.drawable.icon);
                builder.setTitle("自定义输入框");
                builder.setView(textEntryView);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                	
                	EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);
                	EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);
                	showDialog("姓名 :"  + userName.getText().toString()  + "密码:" + password.getText().toString() );
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });
	    break;
	case DIALOG_7:
	    mProgressDialog = new ProgressDialog(this);
	    mProgressDialog.setTitle("读取ing");
	    mProgressDialog.setMessage("正在读取中请稍候");
	    mProgressDialog.setIndeterminate(true);
	    mProgressDialog.setCancelable(true);
	    mProgressDialog.show();
	    return;
	}
	builder.create().show();
    }

    
    private void showDialog(String str) {
	 new AlertDialog.Builder(test.this)
         .setMessage(str)
         .show();
    }
    
    public void run() {
	    int Progress = 0;
	    while(Progress < MAX_PROGRESS) {
		try {
		    Thread.sleep(100);
		    Progress++;  
		    mProgressDialog.incrementProgressBy(1);
		} catch (InterruptedException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		 
	    }
	
    }


 





}

在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助。

1.该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式。

创建dialog对话框方法代码如下:

01 protected void dialog() {
02     AlertDialog.Builder builder = new Builder(Main.this);
03     builder.setMessage("确认退出吗?");
04     builder.setTitle("提示");
05     builder.setPositiveButton("确认", new OnClickListener() {
06      @Override
07      public void onClick(DialogInterface dialog, int which) {
08       dialog.dismiss();
09       Main.this.finish();
10      }
11     });
12     builder.setNegativeButton("取消", new OnClickListener() {
13      @Override
14      public void onClick(DialogInterface dialog, int which) {
15       dialog.dismiss();
16      }
17     });
18     builder.create().show();
19    }
在onKeyDown(int keyCode, KeyEvent event)方法中调用此方法 
1 public boolean onKeyDown(int keyCode, KeyEvent event) {
2     if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
3      dialog();
4     }
5     return false;
6    }
2.改变了对话框的图表,添加了三个按钮

创建dialog的方法代码如下:

01 Dialog dialog = new AlertDialog.Builder(this).setIcon(
02        android.R.drawable.btn_star).setTitle("喜好调查").setMessage(
03        "你喜欢李连杰的电影吗?").setPositiveButton("很喜欢",
04        new OnClickListener() {
05         @Override
06         public void onClick(DialogInterface dialog, int which) {
07          // TODO Auto-generated method stub
08          Toast.makeText(Main.this, "我很喜欢他的电影。",
09            Toast.LENGTH_LONG).show();
10         }
11        }).setNegativeButton("不喜欢", new OnClickListener() {
12       @Override
13       public void onClick(DialogInterface dialog, int which) {
14        // TODO Auto-generated method stub
15        Toast.makeText(Main.this, "我不喜欢他的电影。", Toast.LENGTH_LONG)
16          .show();
17       }
18      }).setNeutralButton("一般", new OnClickListener() {
19       @Override
20       public void onClick(DialogInterface dialog, int which) {
21        // TODO Auto-generated method stub
22        Toast.makeText(Main.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG)
23          .show();
24       }
25      }).create();
26      dialog.show();
3.信息内容是一个简单的View类型

创建dialog方法的代码如下:

1 new AlertDialog.Builder(this).setTitle("请输入").setIcon(
2        android.R.drawable.ic_dialog_info).setView(
3        new EditText(this)).setPositiveButton("确定", null)
4        .setNegativeButton("取消", null).show();
4.信息内容是一组单选框

创建dialog方法的代码如下:

1 new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems(
2        new String[] { "Item1", "Item2" }, null, null)
3        .setPositiveButton("确定", null)
4        .setNegativeButton("取消", null).show();
5.信息内容是一组多选框

创建dialog方法的代码如下:

1 new AlertDialog.Builder(this).setTitle("单选框").setIcon(
2        android.R.drawable.ic_dialog_info).setSingleChoiceItems(
3        new String[] { "Item1", "Item2" }, 0,
4        new DialogInterface.OnClickListener() {
5         public void onClick(DialogInterface dialog, int which) {
6          dialog.dismiss();
7         }
8        }).setNegativeButton("取消", null).show();
6.信息内容是一组简单列表项

创建dialog的方法代码如下:

1 new AlertDialog.Builder(this).setTitle("列表框").setItems(
2        new String[] { "Item1", "Item2" }, null).setNegativeButton(
3        "确定", null).show();
7.信息内容是一个自定义的布局

dialog布局文件代码如下:

01 <?xml version="1.0" encoding="utf-8"?>
02  
03   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
04    android:layout_height="wrap_content" android:layout_width="wrap_content"
05    android:background="#ffffffff" android:orientation="horizontal"
06    android:id="@+id/dialog">
07    <TextView android:layout_height="wrap_content"
08      android:layout_width="wrap_content"
09     android:id="@+id/tvname" android:text="姓名:" />
10    <EditText android:layout_height="wrap_content"
11     android:layout_width="wrap_content" android:id="@+id/etname"android:minWidth="100dip"/>
12   </LinearLayout>
创建dialog方法的代码如下: 
1 LayoutInflater inflater = getLayoutInflater();
2      View layout = inflater.inflate(R.layout.dialog,
3        (ViewGroup) findViewById(R.id.dialog));
4      new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
5        .setPositiveButton("确定", null)
6        .setNegativeButton("取消", null).show();

好了,以上7种Android dialog对话框的使用方法就介绍到这里了,基本都全了,如果大家在android开发过程中遇到dialog的时候就可以拿出来看看。

另外注,本文参考文章: 
http://android.tgbus.com/Android/tutorial/201107/359812.shtml


共有 人打赏支持
粉丝 118
博文 321
码字总数 80011
×
SuShine
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: