1. 程式人生 > >使用AlertDialog.Builder建立對話方塊

使用AlertDialog.Builder建立對話方塊

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展示的對話方塊如圖示: