|
【程序】點擊按鈕後彈出對話框 |
一派掌門 二十級 |
OnClickListener btn2_events = new OnClickListener() { @Override public void onClick(View v) { //Here you must use "MainActivity.this" for the parameter Context. //You can not use "getApplicationContext()" any more! Dialog dlg = new AlertDialog.Builder(MainActivity.this) .setTitle("A question") .setMessage("How many people are there in your home?") .setPositiveButton("Four", null) .setNegativeButton("Three", null) .create(); dlg.show(); } }; findViewById(R.id.button2).setOnClickListener(btn2_events);
|
一派掌門 二十級 |
在界面中創建button2後就可以看到效果
|
|
一派掌門 二十級 |
.setPositiveButton("Four", null) 第二個參數可以設置點擊按鈕後的事件, 一般是new OnClickListener() {},裡面一些事件比如onClick,這個和button2那個差不多
|
|
一派掌門 二十級 |
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
|
|
一派掌門 二十級 |
回覆:5樓 OnClickListener變成了DialogInterface.OnClickListener View v變成了DialogInterface dialog, int which
|
|