Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發中提示框Toast、AlertDialog的使用!

Android開發中提示框Toast、AlertDialog的使用!

編輯:關於Android編程

先了解一下AlertDialog.Builder創建對話框的幾個常用方法:

setTitle() :設置標題
setIcon() :設置圖標
setMessage():設置提示內容
setView() : 設置自定義視圖
setItems() :設置對話框要顯示的一個list
setMultiChoiceItems() :設置對話框顯示的復選框
setNeutralButton() :普通按鈕

setPositiveButton() :添加"Yes"按鈕
setNegativeButton() :添加"No"按鈕
create() : 創建對話框

show() :顯示對話框

下面以一個例子簡單演示一下其應用,先看一下效果圖:

 

Toast的使用效果:

 


AlertDialog的簡單使用:

 


類似於ListView的效果:

 


類似於RadioButton的效果:

 


多選的效果:

 


自定義視圖:

 


代碼示例如下:

一個主activity,兩個布局文件;


ReviewCriteria.java

[java] 
package com.test.restaurantfinder; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 
 
public class ReviewCriteria extends Activity { 
    private static final String tag = "reviewCriteria"; 
    private final String arrayFruit[] = new String[]{"apple","banana","pear"}; 
     
    private Button tishiButton = null;      //提示Toast的按鈕  
    private Button tishiAlert = null;       //普通AlertDialog  
    private Button tishiAlertItem = null;   //類似ListItem  
    private Button AlertRadio = null;       //類似RadioButton  
    private int selectedFruitIndex = 0;      
     
    private Button CheckButton = null;      //類似CheckBox  
    final boolean[] arrayFruitSelected = new boolean[] {false, true, false}; //默認選中  
     
    private Button myCustomButton = null;       //自定義視圖  
    private View myCustomView = null; 
     
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_review_criteria); 
         
        this.tishiButton = (Button)findViewById(R.id.tishiButton); 
        this.tishiAlert = (Button)findViewById(R.id.tishiAlart); 
        this.tishiAlertItem = (Button)findViewById(R.id.AlertItem); 
        this.AlertRadio = (Button)findViewById(R.id.AlertSingleChoice); 
        this.CheckButton = (Button)findViewById(R.id.AlertCheckBox); 
        this.myCustomButton = (Button)findViewById(R.id.customView); 
         
        //Toast  
        this.tishiButton.setOnClickListener( 
                new OnClickListener(){ 
                    @Override 
                    public void onClick(View v){ 
                        Log.i(tag, "in onClick1"); 
                        /*注:在內嵌函數中使用時context 一定要對,不能只是this,而應該是Class.this或者getApplicationContext()*/ 
                        //Toast.makeText(ReviewCriteria.this, "this is a toast", Toast.LENGTH_LONG).show();  
                        Toast toast = Toast.makeText(ReviewCriteria.this, "this is a toast", Toast.LENGTH_SHORT); 
                        toast.setGravity(Gravity.CENTER, 0, 0); 
                        toast.show(); 
                    } 
                }); 
         
        //AlertDialog  
        this.tishiAlert.setOnClickListener( 
                new OnClickListener(){ 
                    public void onClick(View v){ 
                        new AlertDialog.Builder(ReviewCriteria.this). 
                        setIcon(R.drawable.ic_launcher).//圖標  
                        setTitle("Delete?").//標題  
                        setMessage("this is a AlertDialog!").//提示內容  
                        setPositiveButton("Yes", new DialogInterface.OnClickListener() {//確定  
                            @Override 
                            public void onClick(DialogInterface arg0, int arg1) { 
                                //yes to do  
                            } 
                        }).setNegativeButton("No", new DialogInterface.OnClickListener(){//取消  
                            @Override 
                            public void onClick(DialogInterface arg1,int witch){ 
                                //no to do  
                            } 
                        }). 
                        setNeutralButton("More", new DialogInterface.OnClickListener() {//普通確定按鈕  
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                //查看更多  
                            } 
                        }). 
                        show(); 
                    } 
                }); 
         
        //類似ListItem  
        this.tishiAlertItem.setOnClickListener( 
                new OnClickListener(){ 
                    public void onClick(View v){ 
                        new AlertDialog.Builder(ReviewCriteria.this). 
                        setTitle("witch do you like?"). 
                        setItems(arrayFruit,new DialogInterface.OnClickListener() {//Items to choose  
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                Toast.makeText(ReviewCriteria.this, arrayFruit[which], Toast.LENGTH_LONG).show(); 
                            } 
                        }). 
                        setNegativeButton("cancel", new DialogInterface.OnClickListener() {//cancel  
                             
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                //cancel to do  
                            } 
                        }). 
                        show(); 
                    } 
                }); 
        //類似RadioButton  
        this.AlertRadio.setOnClickListener( 
                new OnClickListener(){ 
                    public void onClick(View v){ 
                        new AlertDialog.Builder(ReviewCriteria.this). 
                        setTitle("witch do you like?"). 
                        setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() { 
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                selectedFruitIndex = which; 
                            } 
                        }). 
                        setPositiveButton("OK",new DialogInterface.OnClickListener() { 
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                Toast.makeText(ReviewCriteria.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_LONG).show(); 
                            } 
                        }). 
                        setNegativeButton("Cancel", new DialogInterface.OnClickListener() {//cancel  
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                //cancel to do  
                            } 
                        }). 
                        show(); 
                    } 
                }); 
         
        //類似CheckBox  
        this.CheckButton.setOnClickListener( 
                new OnClickListener(){ 
                    public void onClick(View v){ 
                        new AlertDialog.Builder(ReviewCriteria.this). 
                        setTitle("which do you like?"). 
                        setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() { 
                            @Override 
                            public void onClick(DialogInterface arg0, int which, boolean isChecked) { 
                                arrayFruitSelected[which] = isChecked; 
                            } 
                        }). 
                        setPositiveButton("OK",new DialogInterface.OnClickListener() { 
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                String toastString = "你選中了:"; 
                                for(int i = 0;i < arrayFruit.length; i++){ 
                                    if(arrayFruitSelected[i]){ 
                                        toastString += arrayFruit[i]+"、"; 
                                    } 
                                } 
                                Toast.makeText(ReviewCriteria.this, toastString, Toast.LENGTH_LONG).show(); 
                            } 
                        }). 
                        setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                 
                            } 
                        }). 
                        show(); 
                    } 
                }); 
        //自定義視圖  
        LayoutInflater layoutInflater = LayoutInflater.from(this); 
        myCustomView = layoutInflater.inflate(R.layout.customview, null); 
         
        this.myCustomButton.setOnClickListener( 
                new OnClickListener(){ 
                    public void onClick(View v){ 
                        new AlertDialog.Builder(ReviewCriteria.this). 
                        setTitle("login"). 
                        setView(myCustomView). 
                        setPositiveButton("Login", new DialogInterface.OnClickListener() { 
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                 
                            } 
                        }). 
                        setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
                            @Override 
                            public void onClick(DialogInterface dialog, int which) { 
                                 
                            } 
                        }). 
                        show(); 
                    } 
                }); 
         
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.review_criteria, menu); 
        return true; 
    } 
     

package com.test.restaurantfinder;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ReviewCriteria extends Activity {
 private static final String tag = "reviewCriteria";
 private final String arrayFruit[] = new String[]{"apple","banana","pear"};
 
 private Button tishiButton = null;  //提示Toast的按鈕
 private Button tishiAlert = null;  //普通AlertDialog
 private Button tishiAlertItem = null; //類似ListItem
 private Button AlertRadio = null;  //類似RadioButton
 private int selectedFruitIndex = 0;  
 
 private Button CheckButton = null;  //類似CheckBox
 final boolean[] arrayFruitSelected = new boolean[] {false, true, false}; //默認選中
 
 private Button myCustomButton = null;  //自定義視圖
 private View myCustomView = null;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_review_criteria);
  
  this.tishiButton = (Button)findViewById(R.id.tishiButton);
  this.tishiAlert = (Button)findViewById(R.id.tishiAlart);
  this.tishiAlertItem = (Button)findViewById(R.id.AlertItem);
  this.AlertRadio = (Button)findViewById(R.id.AlertSingleChoice);
  this.CheckButton = (Button)findViewById(R.id.AlertCheckBox);
  this.myCustomButton = (Button)findViewById(R.id.customView);
  
  //Toast
  this.tishiButton.setOnClickListener(
    new OnClickListener(){
     @Override
     public void onClick(View v){
      Log.i(tag, "in onClick1");
      /*注:在內嵌函數中使用時context 一定要對,不能只是this,而應該是Class.this或者getApplicationContext()*/
      //Toast.makeText(ReviewCriteria.this, "this is a toast", Toast.LENGTH_LONG).show();
      Toast toast = Toast.makeText(ReviewCriteria.this, "this is a toast", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
     }
    });
  
  //AlertDialog
  this.tishiAlert.setOnClickListener(
    new OnClickListener(){
     public void onClick(View v){
      new AlertDialog.Builder(ReviewCriteria.this).
      setIcon(R.drawable.ic_launcher).//圖標
      setTitle("Delete?").//標題
      setMessage("this is a AlertDialog!").//提示內容
      setPositiveButton("Yes", new DialogInterface.OnClickListener() {//確定
       @Override
       public void onClick(DialogInterface arg0, int arg1) {
        //yes to do
       }
      }).setNegativeButton("No", new DialogInterface.OnClickListener(){//取消
       @Override
       public void onClick(DialogInterface arg1,int witch){
        //no to do
       }
      }).
      setNeutralButton("More", new DialogInterface.OnClickListener() {//普通確定按鈕
       @Override
       public void onClick(DialogInterface dialog, int which) {
        //查看更多
       }
      }).
      show();
     }
    });
  
  //類似ListItem
  this.tishiAlertItem.setOnClickListener(
    new OnClickListener(){
     public void onClick(View v){
      new AlertDialog.Builder(ReviewCriteria.this).
      setTitle("witch do you like?").
      setItems(arrayFruit,new DialogInterface.OnClickListener() {//Items to choose
       @Override
       public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(ReviewCriteria.this, arrayFruit[which], Toast.LENGTH_LONG).show();
       }
      }).
      setNegativeButton("cancel", new DialogInterface.OnClickListener() {//cancel
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        //cancel to do
       }
      }).
      show();
     }
    });
  //類似RadioButton
  this.AlertRadio.setOnClickListener(
    new OnClickListener(){
     public void onClick(View v){
      new AlertDialog.Builder(ReviewCriteria.this).
      setTitle("witch do you like?").
      setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        selectedFruitIndex = which;
       }
      }).
      setPositiveButton("OK",new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(ReviewCriteria.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_LONG).show();
       }
      }).
      setNegativeButton("Cancel", new DialogInterface.OnClickListener() {//cancel
       @Override
       public void onClick(DialogInterface dialog, int which) {
        //cancel to do
       }
      }).
      show();
     }
    });
  
  //類似CheckBox
  this.CheckButton.setOnClickListener(
    new OnClickListener(){
     public void onClick(View v){
      new AlertDialog.Builder(ReviewCriteria.this).
      setTitle("which do you like?").
      setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
       @Override
       public void onClick(DialogInterface arg0, int which, boolean isChecked) {
        arrayFruitSelected[which] = isChecked;
       }
      }).
      setPositiveButton("OK",new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        String toastString = "你選中了:";
        for(int i = 0;i < arrayFruit.length; i++){
         if(arrayFruitSelected[i]){
          toastString += arrayFruit[i]+"、";
         }
        }
        Toast.makeText(ReviewCriteria.this, toastString, Toast.LENGTH_LONG).show();
       }
      }).
      setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        
       }
      }).
      show();
     }
    });
  //自定義視圖
  LayoutInflater layoutInflater = LayoutInflater.from(this);
  myCustomView = layoutInflater.inflate(R.layout.customview, null);
  
  this.myCustomButton.setOnClickListener(
    new OnClickListener(){
     public void onClick(View v){
      new AlertDialog.Builder(ReviewCriteria.this).
      setTitle("login").
      setView(myCustomView).
      setPositiveButton("Login", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        
       }
      }).
      setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        
       }
      }).
      show();
     }
    });
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.review_criteria, menu);
  return true;
 }
 
}
activity_review_criteria.xml[html] view plaincopyprint?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".ReviewCriteria" > 
 
    <TextView 
        android:id="@+id/title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/hello_world" /> 
     
    <!--show tips by Toast  --> 
    <Button 
        android:id="@+id/tishiButton" 
        android:layout_below="@id/title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="show tips by Toast"/> 
     
    <!--show tips by AlertDialog  --> 
    <Button  
        android:id="@+id/tishiAlart" 
        android:layout_below="@id/tishiButton" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="show tips by AlertDialog"/> 
     
    <!--AlertDialog : Item選擇,類似ListView --> 
    <Button  
        android:id="@+id/AlertItem" 
        android:layout_below="@id/tishiAlart" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="like ListView"/> 
     
    <!--AlertDialog : Item選擇,類似RadioButton  --> 
    <Button  
        android:id="@+id/AlertSingleChoice" 
        android:layout_below="@id/AlertItem" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Like RadioButton"/> 
     
    <!--AlertDialog : Item選擇,類似CheckBox  --> 
    <Button  
        android:id="@+id/AlertCheckBox" 
        android:layout_below="@id/AlertSingleChoice" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Like CheckBox"/> 
     
    <!--AlertDialog : 自定義視圖  --> 
    <Button  
        android:id="@+id/customView" 
        android:layout_below="@id/AlertCheckBox" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="CustomView"/> 
 
</RelativeLayout> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ReviewCriteria" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
   
    <!--show tips by Toast  -->
    <Button
        android:id="@+id/tishiButton"
        android:layout_below="@id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="show tips by Toast"/>
   
    <!--show tips by AlertDialog  -->
    <Button
        android:id="@+id/tishiAlart"
        android:layout_below="@id/tishiButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="show tips by AlertDialog"/>
   
    <!--AlertDialog : Item選擇,類似ListView -->
    <Button
        android:id="@+id/AlertItem"
        android:layout_below="@id/tishiAlart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="like ListView"/>
   
    <!--AlertDialog : Item選擇,類似RadioButton  -->
    <Button
        android:id="@+id/AlertSingleChoice"
        android:layout_below="@id/AlertItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Like RadioButton"/>
   
    <!--AlertDialog : Item選擇,類似CheckBox  -->
    <Button
        android:id="@+id/AlertCheckBox"
        android:layout_below="@id/AlertSingleChoice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Like CheckBox"/>
   
    <!--AlertDialog : 自定義視圖  -->
    <Button
        android:id="@+id/customView"
        android:layout_below="@id/AlertCheckBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="CustomView"/>

</RelativeLayout>
customview.xml[html] view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     
    <LinearLayout  
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 
        <TextView  
            android:id="@+id/usernameTextView" 
            android:text="username:" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/> 
        <EditText  
            android:id="@+id/usernameEdit" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"/> 
         
    </LinearLayout> 
     
    <LinearLayout  
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 
         
        <TextView  
            android:id="@+id/passwordTextView" 
            android:text="password:" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/> 
        <EditText  
            android:id="@+id/passwordEdit" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"/> 
         
    </LinearLayout> 
     
</LinearLayout> 
     

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/usernameTextView"
            android:text="username:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <EditText
            android:id="@+id/usernameEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
       
    </LinearLayout>
   
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
       
        <TextView
            android:id="@+id/passwordTextView"
            android:text="password:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <EditText
            android:id="@+id/passwordEdit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
       
    </LinearLayout>
   
</LinearLayout>
   


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved