UI效果(3): 自訂Dialog

來源:互聯網
上載者:User

在這篇部落格中,你可以瞭解到下面幾項內容:

<1> LayoutInflater 的使用

<2> Dialog、AlertDialog與自訂布局

<3> Button 的onClick屬性

運行,介面就是兩個BUtton

自訂Dialog

自訂AlertDialog

完整源碼:http://download.csdn.net/source/3512363

1. Activity代碼

package mark.zhang;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;public class CustomDialogActivity extends Activity {Builder customAlertDialog = null;Dialog customDialog = null;LayoutInflater inflater = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    }    /** * 為customDialog這個Button設定監聽器 *  * 注意:在xml檔案中設定onClick屬性要與改名稱一致,否則報錯 *  * @param v *            該參數是必需的 */    public void customDialog(View v) {    customDialog = new Dialog(this);     customDialog.setContentView(R.layout.about);    customDialog.setTitle("自訂Dialog");    //該句代碼如果設定為false的話,點擊"返回"按鈕不可以退出該dialog    //customDialog.setCancelable(false);    //View view = inflater.inflate(R.layout.about, (ViewGroup)findViewById(R.id.AboutLinearView));    // 自訂布局中的Button    final Button btn_ok = (Button) customDialog.findViewById(R.id.about_button);    btn_ok.setOnClickListener(new OnClickListener() {// 該事件被觸發@Overridepublic void onClick(View v) {Log.d("mark", "myself dialog!");}});    // 顯示對話方塊    customDialog.show();    }    /** * 為customAlertDialog這個Button設定監聽器 *  * 注意:在xml檔案中設定onClick屬性要與改名稱一致,否則報錯 *  * @param v *            該參數是必需的 */    public void customAlertDialog(View v) {    customAlertDialog = new AlertDialog.Builder(this);     // /layout/about.xml自訂布局檔案    View view = inflater.inflate(R.layout.about, (ViewGroup)findViewById(R.id.AboutLinearView));     customAlertDialog.setTitle("自訂AlertDialog").setView(view);    customAlertDialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.cancel();}});    // 自訂布局中的Button    final Button btn_ok = (Button) view.findViewById(R.id.about_button);    btn_ok.setOnClickListener(new OnClickListener() {// 該事件可以被觸發@Overridepublic void onClick(View v) {Log.d("mark", "myself alertDialog!");// 其它操作,比如結束該Activity或者啟動服務、另一個Activity等}});    // 顯示對話方塊    customAlertDialog.show();    }}

2. main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_marginTop="5dip"    android:text="customDialog"    android:onClick="customDialog"    /><Button      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_marginTop="10dip"    android:text="customAlertDialog"    android:onClick="customAlertDialog"    /></LinearLayout>

這裡需要注意,兩個Button都設定了onClick屬性,看看Activity代碼中有兩個對應的方法。

3. 自訂布局檔案about.xml

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:id="@+id/AboutScrollView"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/AboutLinearView"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical">><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/AboutTextView"android:textColor="?android:attr/textColorPrimaryDisableOnly"android:textAppearance="?android:attr/textAppearanceSmall"android:autoLink="all" android:padding="11dp" android:text="@string/about_text" /><Button android:id="@+id/about_button" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"android:text="ok,I know!" /></LinearLayout></ScrollView>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.