使用AlertDialog.Builder创建对话框

Android 中经常需要使用对话框,对此进行总结。

主要涉及到的对话框有Dialog、AlertDialog、ProgressDialog、TimePickerDialog、DatePickerDialog。

其中AlertDialog是用到的最多的,且内容设置方法最多的类型。

首先,AlertDialog的构造方法是protected类型,故不能直接通过构造方法新建AlertDialog。

如果使用AlertDialog的话:
  1. 使用AlertDialog.Builder
  2. 新建一个对话框类继承至AlertDialog,再使用该对话框类新建
这篇文章主要介绍第一种使用AlertDialog.Builder新建。



在AlertDialog.Builder中分为
  1. 图标区,使用AlertDialog.Builder的setIcon()设置图标
  2. 标题区,使用AlertDialog.Builder的setTitle()或者setCustomTitle()方法设置标题。
  3. 内容区,   使用AlertDialog.Builder的相关方法设置对话框的内容
    • setMessage                        设置对话内容为简单文本内容
    • setItems                              设置对话框内容为简单列表项
    • setSingleChoiceItems         设置对话框内容为单选列表项
    • setMultiChoiceItems           设置对话框内容为多选列表项
    • setAdapter                          设置对话框内容为自定义列表项
    • setView                               设置对话框的内容为自定义View
  4. 按钮区,使用setPositiveButton()  setNegativeButton()  setNeutralButton()设置按钮。

eg:
public class MainActivity extends ActionBarActivity implements OnClickListener {

	String items[];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		items = new String[]{"123","456","789"};

	}

	private void initView() {
		findViewById(R.id.button1).setOnClickListener(this);
		findViewById(R.id.button2).setOnClickListener(this);
		findViewById(R.id.button3).setOnClickListener(this);
		findViewById(R.id.button4).setOnClickListener(this);
		findViewById(R.id.button5).setOnClickListener(this);
		findViewById(R.id.button6).setOnClickListener(this);
	}

	@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);
	}
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this)
				.setIcon(R.drawable.ic_launcher)
				.setTitle("这是标题区")
				.setMessage("这是内容区")
				.setPositiveButton("确认", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
			builder1.create().show();
			break;
		case R.id.button2:
			AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this)
				.setTitle("helloWorld")
				.setIcon(R.drawable.ic_launcher)
				.setItems(items, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast.makeText(MainActivity.this, items[which], 1000).show();
					}
				});
			builder2.create().show();
			break;
		case R.id.button3:
			AlertDialog.Builder builder3 = new AlertDialog.Builder(MainActivity.this)
			.setTitle("helloWorld")
			.setIcon(R.drawable.ic_launcher)
			.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, items[which], 1000).show();
				}
			});
			builder3.create().show();
			break;
		case R.id.button4:
			AlertDialog.Builder builder4 = new AlertDialog.Builder(MainActivity.this)
			.setTitle("helloWorld")
			.setIcon(R.drawable.ic_launcher)
			.setMultiChoiceItems(items, new boolean[]{true, false, true}, new OnMultiChoiceClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					// TODO Auto-generated method stub
					
				}
			});
			builder4.create().show();
			break;
		case R.id.button5:
			AlertDialog.Builder builder5 = new AlertDialog.Builder(MainActivity.this)
			.setTitle("helloWorld")
			.setIcon(R.drawable.ic_launcher)
			.setView(new EditText(this))
			.setPositiveButton("确定", null);
			
			builder5.create().show();
			break;
		case R.id.button6:
			View ly = getLayoutInflater().inflate(R.layout.dialog, null);
			AlertDialog.Builder builder6 = new AlertDialog.Builder(MainActivity.this)
			.setTitle("helloWorld")
			.setIcon(R.drawable.ic_launcher)
			.setView(ly);
			builder6.create().show();
			break;
		}
	}
}

button1展示的对话框如图示:

button2展示的对话框如图示:

button3展示的对话框如图示:

button4展示的对话框如图示:

button5展示的对话框如图示:

button6展示的对话框如图示:



附:为什么create()和show()没有和set的方法连写?
答:create和show返回的是AlertDialog类型,而set方法返回的是AlertDialog.Builder类型。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值