Android开发中提示框Toast、AlertDialog的使用!

Android开发中提示框经常使用,今天看着API,将一些经常用到的提示形式总结了一下,仅供参考!

转发请注明作者和出处:大飞_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

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
<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
<?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>
    








  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio,有两种常用的消息提示框可以使用Toast提示框AlertDialog。这两种提示框在功能和使用方式上有所不同。 1. Toast提示框是一种简单的消息提示框,用于向用户显示一条临时性的消息。它的代码示例如下: Toast.makeText(MainActivity.this, "有空输入!\n请重新输入!", Toast.LENGTH_SHORT).show(); 这段代码会在底部弹出一个短暂显示的提示框,其包含有关输入错误的消息。Toast提示框适用于简单的文本提示,对于输入错误等情况较为合适。 2. AlertDialog是一种更为复杂和功能丰富的提示框,可以用于实现多种功能,例如提示说明、单选框、复选框甚至登录功能。AlertDialog可以通过编写XML文件来实现更多的功能和更好的界面。下面是一个使用AlertDialog的示例代码: Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { //放在UI线程弹AlertDialog的代码 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示") .setMessage("有空输入!\n请重新输入!") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //确定按钮的点击事件处理 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //取消按钮的点击事件处理 } }) .show(); } }); 这段代码会创建一个AlertDialog并在UI线程显示出来。其,通过AlertDialog.Builder来构建对话框的内容,可以设置标题、消息内容以及确定和取消按钮的点击事件。通过调用show()方法来显示对话框。 总结来说,Toast提示框适用于简单的文本提示,而AlertDialog则可以实现更多的功能和更好的界面。具体选择哪种消息提示框取决于实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值