Android---AlertDialog
生活随笔
收集整理的這篇文章主要介紹了
Android---AlertDialog
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼如下:
//創(chuàng)建一個(gè)AlertDialogAlertDialog.Builder ad=new AlertDialog.Builder(this);//標(biāo)題ad.setTitle(R.string.alert_dialog_title_txt);//內(nèi)容ad.setMessage(R.string.alert_dialog_message_txt);// Positive Buttonad.setPositiveButton(R.string.alert_dialog_positive_txt, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {TODO: 15/6/3Log.d(TAG,"++++++用戶點(diǎn)擊了 Positive Button");}});// Negative Buttonad.setNegativeButton(R.string.alert_dialog_negative_txt, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {TODO: 15/6/3Log.d(TAG,"-------用戶點(diǎn)擊了 Negative Button");}});//Cancel Button/**ad.setCancelable(boolean b);* 用戶在沒有做任何選擇而按下“返回鍵”時(shí)是否應(yīng)該關(guān)閉AlertDialog* true :可以關(guān)閉* false : 不可以關(guān)閉* *///AlertDialog可以取消ad.setCancelable(true);ad.setOnCancelListener(new DialogInterface.OnCancelListener() {@Overridepublic void onCancel(DialogInterface dialog) {TODO: 15/6/3Log.d(TAG,"******用戶選擇了取消");}});//AlertDialog showad.show();
效果如下:
當(dāng)用戶點(diǎn)擊“Yes” ,“No”和物理返回鍵時(shí),可以觸發(fā)相應(yīng)的事件:
1》當(dāng)用戶點(diǎn)擊了“Yes”時(shí):
06-04 10:14:14.719 31083-31083/com.demo3.cxc.actionbardemo3 D/MainActivity﹕ ++++++用戶點(diǎn)擊了 Positive Button2》當(dāng)用戶點(diǎn)擊了“No”時(shí):
06-04 10:15:10.559 31083-31083/com.demo3.cxc.actionbardemo3 D/MainActivity﹕ -------用戶點(diǎn)擊了 Negative Button
3》當(dāng)用戶點(diǎn)擊返加鍵時(shí):
06-04 10:15:45.329 31083-31083/com.demo3.cxc.actionbardemo3 D/MainActivity﹕ ******用戶選擇了取消
總結(jié)
以上是生活随笔為你收集整理的Android---AlertDialog的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式---命令模式
- 下一篇: Android---自定义Toast