2012-04-23 6 views
0

を追加する方法について相談したい:アンドロイド:私は、ダイアログを作るため、このコードを持っているダイアログのボタンのジレンマ

Dialog d = new Dialog(this); 
d.setTitle("AA"); 
TextView tv=new TextView(this); 
tv.setText("BB"); 
d.setContentView(tv); 
d.show(); 

私は、このダイアログには2つのボタンを追加し、ボタン押下イベントをキャッチする方法を探していますか?

おかげ

+0

を? http://www.codeofaninja.com/2011/07/android-alertdialog-example.html – zapl

答えて

2

私が代わりにDialogコンストラクタのAlertDialog.Builder使用します。このような

AlertDilog ad = new AlertDialog.Builder(this).create(); 
ad.setTitle(R.string.app_name); 
ad.setMessage(this.getString(R.string.dialog_message); 
ad.setCancelable(true); 

ad.setButton(AlertDialog.BUTTON1, this.getString(R.string.first_btn_label), new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     //Do something when the first button is pressed 
     dismissDialog(DIALOG_SOLVED); 
    } 
}); 

ad.setButton(AlertDialog.BUTTON2, this.getString(R.string.second_btn_label), new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     //Do something when the second button is pressed 
     dismissDialog(DIALOG_SOLVED); 
    } 
}); 

//You can even add a third button if you want to 
/* 
ad.setButton(AlertDialog.BUTTON3, this.getString(R.string.third_btn_label), new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     //Do something when the third button is pressed 
     dismissDialog(DIALOG_SOLVED); 
    } 
}); 
*/ 

ad.show(); 
関連する問題