首页 > 代码库 > (Android review)dialog的使用

(Android review)dialog的使用


一、基本知识点

常见的dialog


基本代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.create();
dialog.show();


1)常见对话框
builder.setMessage("浏览传智播客的网站");
builder.setPositiveButton



2)选择对话框
builder.setItems(items, new DialogInterface.OnClickListener()

items是一个选项内容的数组



3)单选对话框
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() 


第二个参数:默认选中的按钮



4)多选对话框
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() 



5)自定义对话框
LayoutInflater mInflater = LayoutInflater.from(this);
 View view = mInflater.inflate(R.layout.custom_dialog, null);


builder.setView(view);





二、示例代码

1、MainActivity

package com.example.dialogtest;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
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.main);
	}

	public void commondialog(View view){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCancelable(false);
		builder.setTitle("女神");
		builder.setMessage("章泽天是你的女神吗?");
		builder.setPositiveButton("确定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				
			}
		});
	
		builder.setNeutralButton("隐藏", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
		
		builder.setNegativeButton("取消", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {

				
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public void selectdialog(View view){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		final String items[] = new String[]{
			"章泽天",
			"章泽天1",
			"章泽天2",
			"章泽天3"
		};
		builder.setTitle("选择女神");
		builder.setItems(items, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), items[which], 0).show();
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
		
	}
	
	public void singleselectdialog(View view){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    
		builder.setTitle("选择女神");
		
		final String items[] = new String[]{
			"章泽天",
			"章泽天1",
			"章泽天2",
			"章泽天3"
			
		};
		
		builder.setSingleChoiceItems(items, 0, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), items[which], 0).show();
			}
		});
		
		builder.setPositiveButton("确定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public void multiselectdialog(View view){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		final String items[] = new String[]{
			"章泽天",
			"章泽天1",
			"章泽天2",
			"章泽天3",
		};
		final boolean checkedItems[] = new boolean[]{
				true,false,false,false
		};
		
		builder.setTitle("选择女神");
		builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				Toast.makeText(getApplicationContext(), items[which], 0).show();
			}
		}); 
		
		builder.setPositiveButton("确定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public void customdialog(View view){
		LayoutInflater inflater = LayoutInflater.from(this);
		View mView = inflater.inflate(R.layout.custom_dialog, null);
		
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("请输入用户信息");
		builder.setView(mView);
		
		final AlertDialog dialog = builder.create();
	    dialog.show();
	    
		Button okBtn = (Button) mView.findViewById(R.id.bt_ok);
		okBtn.setOnClickListener(new View.OnClickListener() {//注意,这个demo用到了两个OnClickListener.所以这里需要加上View
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
	}
	
	@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;
	}

}


三、源码下载

http://download.csdn.net/detail/caihongshijie6/7799073


四、效果图