Android:普通對話方塊Dialog

來源:互聯網
上載者:User

標籤:extend   login   圖片   span   產生   odi   positive   cli   turn   

String.xml檔案的代碼如下:
1 <resources>2     <string name="app_name">普通對話方塊案例</string>3     <string name="btn">顯示普通對話方塊</string>4     <string name="title">普通對話方塊</string>5     <string name="ok">確定</string>6     <string name="dialog_msg">這是普通對話方塊中的內容!!!</string>7 </resources>
activity_common_dialog代碼如下:
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3     xmlns:android="http://schemas.android.com/apk/res/android" 4     android:orientation="vertical" 5     android:layout_width="fill_parent" 6     android:layout_height="fill_parent"> 7  8     <EditText 9         android:text=""10         android:id="@+id/EidtText01"11         android:cursorVisible="false"12         android:layout_width="fill_parent"13         android:layout_height="wrap_content" />14 15     <Button16         android:id="@+id/Button01"17         android:text="@string/btn"18         android:layout_width="fill_parent"19         android:layout_height="wrap_content" />20 21 </LinearLayout>
java檔案代碼如下:
 1 package com.example.sample_6_3; 2  3 import android.app.Dialog; 4 import android.content.DialogInterface; 5 import android.support.v7.app.AlertDialog; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.Button;10 import android.widget.EditText;11 12 public class CommonDialogActivity extends AppCompatActivity {13     final int COMMON_DIALOG = 1;14     @Override15     protected void onCreate(Bundle savedInstanceState) {16         super.onCreate(savedInstanceState);17         setContentView(R.layout.activity_common_dialog);18         Button btn = (Button) findViewById(R.id.Button01);19         btn.setOnClickListener(new View.OnClickListener() {20             @Override21             public void onClick(View v) {22                 showDialog(COMMON_DIALOG);          //顯示普通對話方塊23             }24         });25     }26     protected Dialog onCreateDialog(int id) {27         Dialog dialog = null;          //聲明一個dialog對象用於返回28         switch (id) {                //對id進行判斷29             case COMMON_DIALOG:30                 AlertDialog.Builder b = new AlertDialog.Builder(this);31                 b.setIcon(R.drawable.dialog);                   //設定對話方塊表徵圖32                 b.setTitle(R.string.title);                    //設定對話方塊標題33                 b.setMessage(R.string.dialog_msg);           //設定對話方塊顯示內容34                 b.setPositiveButton(35                         R.string.ok,36                         new DialogInterface.OnClickListener() {37                             @Override38                             public void onClick(DialogInterface dialog, int which) {39                                 EditText et = (EditText) findViewById(R.id.EidtText01);40                                 et.setText(R.string.dialog_msg);        //設定EditText內容41                             }42                         });43                 dialog = b.create();              //產生dialog對象44                 break;45             default:46                 break;47         }48         return dialog;                  //返回生產dialog的對象49     }50 }

 

效果如:

Android:普通對話方塊Dialog

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.