閱讀92 返回首頁    go 阿裏雲 go 技術社區[雲棲]


AlertDialog使用實例

 

創建項目AlertDialogDemo如圖

image

AlertDialogDemo.java文件代碼如下:

01 package zerone.AlertDialogDemo;
02   
03 import android.app.Activity;
04   
05 public class AlertDialogDemo extends Activity {
06     /** Called when the activity is first created. */
07     final int DIALOG_WELCOME = 1;
08     private Button btn_alert;
09     @Override
10     public void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.main);
13         btn_alert=(Button)findViewById(R.id.btn_dialog);
14         btn_alert.setOnClickListener(new View.OnClickListener() {
15             @Override
16             public void onClick(View v) {
17                 showDialog(DIALOG_WELCOME);//調用onCreateDialog
18             }
19         });
20     }
21       
22     @Override
23     protected Dialog onCreateDialog(int id, Bundle args) {
24         switch (id) {
25         case DIALOG_WELCOME:
26             return new AlertDialog.Builder(AlertDialogDemo.this)
27             .setTitle("歡迎").setMessage("歡迎使用本程序")
28             .setIcon(android.R.drawable.ic_dialog_info)
29             .setPositiveButton("確定", new OnClickListener() {
30                 @Override
31                 public void onClick(DialogInterface dialog, int which) {
32                     Toast.makeText(AlertDialogDemo.this,"點擊\"確定\"按鈕後", Toast.LENGTH_SHORT).show();
33                 }
34             }).create();
35         default:
36             return null;
37         }
38     }
39       
40 }

main.xml文件如下:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent"
06     >
07 <TextView  
08     android:layout_width="fill_parent" 
09     android:layout_height="wrap_content" 
10     android:text="@string/app_title"
11     />
12 <Button
13    android:id="@+id/btn_dialog"
14    android:layout_width="fill_parent"
15    android:layout_height="wrap_content"
16    android:text="彈出"/>
17 </LinearLayout>
1  

運行實例效果截圖:

imageimageimage

最後更新:2017-04-02 06:51:53

  上一篇:go Chrome(穀歌)瀏覽器擴展開發初探
  下一篇:go java定時器的具體使用和web使用