AlertDialogue & Toast

来源:互联网 发布:极品飞车20优化 编辑:程序博客网 时间:2024/06/06 03:01


转自:大飞_Rflyee-http://blog.csdn.net/rflyee/article/details/8978100


先了解一下AlertDialog.Builder创建对话框的几个常用方法:

setTitle() :设置标题
setIcon() :设置图标
setMessage():设置提示内容
setView() : 设置自定义视图
setItems() :设置对话框要显示的一个list
setMultiChoiceItems() :设置对话框显示的复选框
setNeutralButton() :普通按钮

setPositiveButton() :添加"Yes"按钮
setNegativeButton() :添加"No"按钮
create() : 创建对话框

show() :显示对话框

下面以一个例子简单演示一下其应用,先看一下效果图:

Toast的使用效果:

AlertDialog的简单使用:

类似于ListView的效果:

类似于RadioButton的效果:

多选的效果:

自定义视图:

代码示例如下:

一个主activity,两个布局文件;

ReviewCriteria.java

[java] view plaincopy
  1. package com.test.restaurantfinder;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.DialogInterface;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.Gravity;  
  9. import android.view.LayoutInflater;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;  
  15.   
  16. public class ReviewCriteria extends Activity {  
  17.     private static final String tag = "reviewCriteria";  
  18.     private final String arrayFruit[] = new String[]{"apple","banana","pear"};  
  19.       
  20.     private Button tishiButton = null;      //提示Toast的按钮  
  21.     private Button tishiAlert = null;       //普通AlertDialog  
  22.     private Button tishiAlertItem = null;   //类似ListItem  
  23.     private Button AlertRadio = null;       //类似RadioButton  
  24.     private int selectedFruitIndex = 0;       
  25.       
  26.     private Button CheckButton = null;      //类似CheckBox  
  27.     final boolean[] arrayFruitSelected = new boolean[] {falsetruefalse}; //默认选中  
  28.       
  29.     private Button myCustomButton = null;       //自定义视图  
  30.     private View myCustomView = null;  
  31.       
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.activity_review_criteria);  
  36.           
  37.         this.tishiButton = (Button)findViewById(R.id.tishiButton);  
  38.         this.tishiAlert = (Button)findViewById(R.id.tishiAlart);  
  39.         this.tishiAlertItem = (Button)findViewById(R.id.AlertItem);  
  40.         this.AlertRadio = (Button)findViewById(R.id.AlertSingleChoice);  
  41.         this.CheckButton = (Button)findViewById(R.id.AlertCheckBox);  
  42.         this.myCustomButton = (Button)findViewById(R.id.customView);  
  43.           
  44.         //Toast  
  45.         this.tishiButton.setOnClickListener(  
  46.                 new OnClickListener(){  
  47.                     @Override  
  48.                     public void onClick(View v){  
  49.                         Log.i(tag, "in onClick1");  
  50.                         /*注:在内嵌函数中使用时context 一定要对,不能只是this,而应该是Class.this或者getApplicationContext()*/  
  51.                         //Toast.makeText(ReviewCriteria.this, "this is a toast", Toast.LENGTH_LONG).show();  
  52.                         Toast toast = Toast.makeText(ReviewCriteria.this"this is a toast", Toast.LENGTH_SHORT);  
  53.                         toast.setGravity(Gravity.CENTER, 00);  
  54.                         toast.show();  
  55.                     }  
  56.                 });  
  57.           
  58.         //AlertDialog  
  59.         this.tishiAlert.setOnClickListener(  
  60.                 new OnClickListener(){  
  61.                     public void onClick(View v){  
  62.                         new AlertDialog.Builder(ReviewCriteria.this).  
  63.                         setIcon(R.drawable.ic_launcher).//图标  
  64.                         setTitle("Delete?").//标题  
  65.                         setMessage("this is a AlertDialog!").//提示内容  
  66.                         setPositiveButton("Yes"new DialogInterface.OnClickListener() {//确定  
  67.                             @Override  
  68.                             public void onClick(DialogInterface arg0, int arg1) {  
  69.                                 //yes to do  
  70.                             }  
  71.                         }).setNegativeButton("No"new DialogInterface.OnClickListener(){//取消  
  72.                             @Override  
  73.                             public void onClick(DialogInterface arg1,int witch){  
  74.                                 //no to do  
  75.                             }  
  76.                         }).  
  77.                         setNeutralButton("More"new DialogInterface.OnClickListener() {//普通确定按钮  
  78.                             @Override  
  79.                             public void onClick(DialogInterface dialog, int which) {  
  80.                                 //查看更多  
  81.                             }  
  82.                         }).  
  83.                         show();  
  84.                     }  
  85.                 });  
  86.           
  87.         //类似ListItem  
  88.         this.tishiAlertItem.setOnClickListener(  
  89.                 new OnClickListener(){  
  90.                     public void onClick(View v){  
  91.                         new AlertDialog.Builder(ReviewCriteria.this).  
  92.                         setTitle("witch do you like?").  
  93.                         setItems(arrayFruit,new DialogInterface.OnClickListener() {//Items to choose  
  94.                             @Override  
  95.                             public void onClick(DialogInterface dialog, int which) {  
  96.                                 Toast.makeText(ReviewCriteria.this, arrayFruit[which], Toast.LENGTH_LONG).show();  
  97.                             }  
  98.                         }).  
  99.                         setNegativeButton("cancel"new DialogInterface.OnClickListener() {//cancel  
  100.                               
  101.                             @Override  
  102.                             public void onClick(DialogInterface dialog, int which) {  
  103.                                 //cancel to do  
  104.                             }  
  105.                         }).  
  106.                         show();  
  107.                     }  
  108.                 });  
  109.         //类似RadioButton  
  110.         this.AlertRadio.setOnClickListener(  
  111.                 new OnClickListener(){  
  112.                     public void onClick(View v){  
  113.                         new AlertDialog.Builder(ReviewCriteria.this).  
  114.                         setTitle("witch do you like?").  
  115.                         setSingleChoiceItems(arrayFruit, 0new DialogInterface.OnClickListener() {  
  116.                             @Override  
  117.                             public void onClick(DialogInterface dialog, int which) {  
  118.                                 selectedFruitIndex = which;  
  119.                             }  
  120.                         }).  
  121.                         setPositiveButton("OK",new DialogInterface.OnClickListener() {  
  122.                             @Override  
  123.                             public void onClick(DialogInterface dialog, int which) {  
  124.                                 Toast.makeText(ReviewCriteria.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_LONG).show();  
  125.                             }  
  126.                         }).  
  127.                         setNegativeButton("Cancel"new DialogInterface.OnClickListener() {//cancel  
  128.                             @Override  
  129.                             public void onClick(DialogInterface dialog, int which) {  
  130.                                 //cancel to do  
  131.                             }  
  132.                         }).  
  133.                         show();  
  134.                     }  
  135.                 });  
  136.           
  137.         //类似CheckBox  
  138.         this.CheckButton.setOnClickListener(  
  139.                 new OnClickListener(){  
  140.                     public void onClick(View v){  
  141.                         new AlertDialog.Builder(ReviewCriteria.this).  
  142.                         setTitle("which do you like?").  
  143.                         setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {  
  144.                             @Override  
  145.                             public void onClick(DialogInterface arg0, int which, boolean isChecked) {  
  146.                                 arrayFruitSelected[which] = isChecked;  
  147.                             }  
  148.                         }).  
  149.                         setPositiveButton("OK",new DialogInterface.OnClickListener() {  
  150.                             @Override  
  151.                             public void onClick(DialogInterface dialog, int which) {  
  152.                                 String toastString = "你选中了:";  
  153.                                 for(int i = 0;i < arrayFruit.length; i++){  
  154.                                     if(arrayFruitSelected[i]){  
  155.                                         toastString += arrayFruit[i]+"、";  
  156.                                     }  
  157.                                 }  
  158.                                 Toast.makeText(ReviewCriteria.this, toastString, Toast.LENGTH_LONG).show();  
  159.                             }  
  160.                         }).  
  161.                         setNegativeButton("Cancel"new DialogInterface.OnClickListener() {  
  162.                             @Override  
  163.                             public void onClick(DialogInterface dialog, int which) {  
  164.                                   
  165.                             }  
  166.                         }).  
  167.                         show();  
  168.                     }  
  169.                 });  
  170.         //自定义视图  
  171.         LayoutInflater layoutInflater = LayoutInflater.from(this);  
  172.         myCustomView = layoutInflater.inflate(R.layout.customview, null);  
  173.           
  174.         this.myCustomButton.setOnClickListener(  
  175.                 new OnClickListener(){  
  176.                     public void onClick(View v){  
  177.                         new AlertDialog.Builder(ReviewCriteria.this).  
  178.                         setTitle("login").  
  179.                         setView(myCustomView).  
  180.                         setPositiveButton("Login"new DialogInterface.OnClickListener() {  
  181.                             @Override  
  182.                             public void onClick(DialogInterface dialog, int which) {  
  183.                                   
  184.                             }  
  185.                         }).  
  186.                         setNegativeButton("Cancel"new DialogInterface.OnClickListener() {  
  187.                             @Override  
  188.                             public void onClick(DialogInterface dialog, int which) {  
  189.                                   
  190.                             }  
  191.                         }).  
  192.                         show();  
  193.                     }  
  194.                 });  
  195.           
  196.     }  
  197.   
  198.     @Override  
  199.     public boolean onCreateOptionsMenu(Menu menu) {  
  200.         // Inflate the menu; this adds items to the action bar if it is present.  
  201.         getMenuInflater().inflate(R.menu.review_criteria, menu);  
  202.         return true;  
  203.     }  
  204.       
  205. }  
activity_review_criteria.xml
[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".ReviewCriteria" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/title"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/hello_world" />  
  16.       
  17.     <!--show tips by Toast  -->  
  18.     <Button  
  19.         android:id="@+id/tishiButton"  
  20.         android:layout_below="@id/title"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="show tips by Toast"/>  
  24.       
  25.     <!--show tips by AlertDialog  -->  
  26.     <Button   
  27.         android:id="@+id/tishiAlart"  
  28.         android:layout_below="@id/tishiButton"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:text="show tips by AlertDialog"/>  
  32.       
  33.     <!--AlertDialog : Item选择,类似ListView -->  
  34.     <Button   
  35.         android:id="@+id/AlertItem"  
  36.         android:layout_below="@id/tishiAlart"  
  37.         android:layout_width="fill_parent"  
  38.         android:layout_height="wrap_content"  
  39.         android:text="like ListView"/>  
  40.       
  41.     <!--AlertDialog : Item选择,类似RadioButton  -->  
  42.     <Button   
  43.         android:id="@+id/AlertSingleChoice"  
  44.         android:layout_below="@id/AlertItem"  
  45.         android:layout_width="fill_parent"  
  46.         android:layout_height="wrap_content"  
  47.         android:text="Like RadioButton"/>  
  48.       
  49.     <!--AlertDialog : Item选择,类似CheckBox  -->  
  50.     <Button   
  51.         android:id="@+id/AlertCheckBox"  
  52.         android:layout_below="@id/AlertSingleChoice"  
  53.         android:layout_width="fill_parent"  
  54.         android:layout_height="wrap_content"  
  55.         android:text="Like CheckBox"/>  
  56.       
  57.     <!--AlertDialog : 自定义视图  -->  
  58.     <Button   
  59.         android:id="@+id/customView"  
  60.         android:layout_below="@id/AlertCheckBox"  
  61.         android:layout_width="fill_parent"  
  62.         android:layout_height="wrap_content"  
  63.         android:text="CustomView"/>  
  64.   
  65. </RelativeLayout>  
customview.xml
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.       
  8.     <LinearLayout   
  9.         android:orientation="horizontal"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content">  
  12.         <TextView   
  13.             android:id="@+id/usernameTextView"  
  14.             android:text="username:"  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"/>  
  17.         <EditText   
  18.             android:id="@+id/usernameEdit"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"/>  
  21.           
  22.     </LinearLayout>  
  23.       
  24.     <LinearLayout   
  25.         android:orientation="horizontal"  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content">  
  28.           
  29.         <TextView   
  30.             android:id="@+id/passwordTextView"  
  31.             android:text="password:"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"/>  
  34.         <EditText   
  35.             android:id="@+id/passwordEdit"  
  36.             android:layout_width="fill_parent"  
  37.             android:layout_height="wrap_content"/>  
  38.           
  39.     </LinearLayout>  
  40.       
  41. </LinearLayout>  
  42.       
0 0
原创粉丝点击