Android对话框(一)AlertDialog

最近在做项目,小组几个回了家。界面暂时没人做,用到自定义对话框只能临时去学。现在把对话框的相关整理。

<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="com.example.alertdialog.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="显示AlertDialog对话框" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:text="显示列表对话框" />

    <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:text="显示单选列表对话框" />

    <Button
        android:id="@+id/button4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:text="显示多选对话框" />

    <Button
        android:id="@+id/button5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button4"
        android:layout_centerHorizontal="true"
        android:text="调用资源文件多选对话框" />

</RelativeLayout>
package com.example.alertdialog;

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

public class MainActivity extends Activity {

	private Button button1;
	private Button button2;
	private final CharSequence[] items = {"广州","深圳","上海","北京"}; 
	private Button button3;
	private Button button4;
	private Button button5;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button4 = (Button) findViewById(R.id.button4);
		button5 = (Button) findViewById(R.id.button5);
		button1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View view) {
				AlertDialog.Builder  builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("提示");
				builder.setMessage("你确定要删除吗!");
				builder.setIcon(R.drawable.ic_launcher);
				builder.setPositiveButton("确定", new  DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int arg1) {
						// 让对话框消失
						dialog.dismiss();
					}
				});
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						
					}
				});
				builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						//忽略用户操作,作用和取消一样
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("提示");
				//不能设置setMessage的内容,不然会覆盖原来item的值
				//在OnClickListener前要加DialogInterface,不然会报错
				builder.setItems(items, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int index) {
						//item[index]不能转化为String,转化为CharSequence就行了
						CharSequence select_item = items[index];
						Toast.makeText(MainActivity.this, "你选择了城市"+select_item, 1).show();
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});
		button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("请选择以下城市");
				builder.setIcon(R.drawable.ic_launcher);
				/*
				 * 第一个参数:显示在对话框中供选择列表
				 * 第二个参数:默认选择的值,-1表示一个都没选
				 * 第三个参数:监听器
				 */
	            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						CharSequence select_item = items[which];
						Toast.makeText(MainActivity.this, "-->>"+select_item, 1).show();
						dialog.dismiss();
					}
				});
				AlertDialog alertDialog = builder.create();
	            alertDialog.show();
			}
		});
		button4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("请选择以下城市");
				/*
				 * 第一个参数:供选择多选列表
				 * 第二个参数:boolean类型的数组,表示选中的项;如设置为null,表示没有选项默认被选中
				 * 第三个参数:不用说,监听器
				 */
				builder.setMultiChoiceItems(items, new boolean[]{false,false,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1, boolean ischecked) {
						// TODO Auto-generated method stub
					}
				});
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// 直接获取选择的值
						
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});
		//其实是button4的另一种写法
		button5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("请选择以下城市");
				//获取选择值:使用StringBuffer追加,中间用逗号分隔开来
				builder.setMultiChoiceItems(R.array.city,  new boolean[]{false,false,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
					public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
						// TODO Auto-generated method stub
					}
				});
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						// 获取选择的值
						
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
		});
	}
}
技术分享技术分享技术分享技术分享技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。