Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框

1.AlertDialog类的功能特别强大,不仅可以生成带有按钮的提示对话框,还可以生成带列表的列表对话框,在使用AlertDialog类生成对话框时,只能生成带按钮的提示对话框,如果要生成带列表的提示对话框的话,需要使用AlertDialog.Builder类,接下来将介绍如何通过AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框。

2.首先,新建一个安卓项目,项目名为DialogTest,接下来布局所要显示的页面,打开res目录下的layout文件夹下的activity_main.xml文件,界面很简单,就一个按钮,点击此按钮弹出带取消,确定,中立的提示对话框,附上布局文件的代码:

activity_main.xml文件:

<LinearLayout 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:orientation="vertical">
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/showDialog"/>
    
</LinearLayout>

3.接下来开始写如何创建提示对话框的相应代码,不多说,直接附上MainActivity.java文件的代码,里面有简单的注释,其中如果要使用AlertDialog类来创建对话框时,就在按钮点击事件时调用openDialog1()方法,这里默认使用AlertDialog.Builder类来创建提示对话框,在点击事件里调用openDialog()方法:

MainActivity.java文件:

package com.dialogtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button1=(Button)findViewById(R.id.button1);//获取按钮控件
		
		//点击按钮时,进行事件处理
		button1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				openDialog();//使用AlertDialog.Builder类打开对话框的方法
				//openDialog1();//使用AlertDialog类打开对话框的方法
			}
			
		});
	}
	
	/*使用AlertDialog.Builder类创建一个带有取消,确定,中立的对话框*/
	protected void openDialog(){
		AlertDialog.Builder builder=new Builder(MainActivity.this);//使用AlertDialog.Builder类实例化一个对话框
		builder.setIcon(R.drawable.ic_launcher);//设置对话框的图标
		builder.setTitle("对话框...");//设置对话框的标题
		builder.setMessage("带取消、中立和确定按钮的对话框!!!");//设置要显示的内容
		
		//添加取消按钮
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框
			}
		}); 
		
		//添加确定按钮
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "你点击了确定按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框
			}
		});
		
		//添加中立按钮
		builder.setNeutralButton(R.string.neutral, new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
			}
			
		});
		
		builder.create().show();//创建对话框并显示
	}
	
	/*使用AlertDialog类创建一个带有取消,确定,中立的对话框*/
	public void openDialog1(){
		AlertDialog alert=new AlertDialog.Builder(MainActivity.this).create();//使用AlertDialog类实例化一个对话框并创建对话框
		alert.setIcon(R.drawable.ic_launcher);//设置对话框的图标
		alert.setTitle("对话框...");//设置对话框的标题
		alert.setMessage("带取消、中立和确定按钮的对话框!!!");//设置要显示的内容
		
		//添加取消按钮
		alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框
			}
		});
		
		//添加确定按钮
		alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "你点击了确定按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框
			}
		});
		
		//添加中立按钮
		alert.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
			}
		});
		alert.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.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}

4.使用AlertDialog类和AlertDialog.Builder类创建提示对话框的效果是一样的,但是创建提示对话框的代码有区别,区别在于实例化的类不同。

(1).AlertDialog类里创建对话框时:

AlertDialog alert=new AlertDialog.Builder(MainActivity.this).create();//使用AlertDialog类实例化一个对话框并创建对话框


生成按钮时是用setButton方法,其中添加取消按钮时代码如下:

//添加取消按钮
		alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框
			}
		});


后面必须把这个提示对话框显示在界面当中,使用下面的代码:

alert.show();//显示对话框


(2).AlertDialog.Builder类里创建对话框时,只是实例化了个类,并没有创建出来:

AlertDialog.Builder builder=new Builder(MainActivity.this);//使用AlertDialog.Builder类实例化一个对话框


生成按钮时是用setNegativeButton(),setNeutralButton(),setPositiveButton()方法分别为对话框添加取消按钮,中立按钮和确定按钮,其中添加取消按钮时代码如下:

//添加取消按钮
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框
			}
		}); 


后面必须把这个提示对话框创建并显示出来,使用下面这一行代码:

builder.create().show();//创建对话框并显示


5.接下来附上这里所需要的字符串资源,附上res目录下values文件夹的strings.xml文件:

strings.xml文件:

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

    <string name="app_name">DialogTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="showDialog">点击按钮显示带取消,确定,中立的对话框</string>
    <string name="neutral">中立</string>
</resources>

6.接下来运行此安卓项目,运行效果如下:

Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框_第1张图片

点击此按钮,出现效果如下:

Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框_第2张图片

其中有我在MainActivity.java文件设置提示对话框的图片,标题,显示的信息和为此对话框添加的三个按钮,点击提示对话框里的取消按钮,效果如下:

Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框_第3张图片

点击对话框里的中立按钮,在为对话框添加按钮时没做任何事件处理,所以点击之后效果如下:
Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框_第4张图片

点击提示对话框里的确定按钮,效果如下:

Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框_第5张图片


7,以上就是这篇博客的内容,仅供大家学习参考,写得不好,请见谅,如果有什么错误请指出,谢谢!

你可能感兴趣的:(android,安卓,对话框)