天天看點

自定義控件(二)自定義Dialog

本節要實作:自定義一個Dialog

結果如下:

自定義控件(二)自定義Dialog

步 驟

1.配置register_dialog.xml:

下面是一個自定義的dialog。

功能是:點選dialog所依附的activity上的“注冊”按鈕,彈出此對話框。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fff" >
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:text="确認手機号碼"
        android:textColor="#000"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="30dp"
        android:text="我們将發送驗證碼短信到這個号碼:\n+86 XXXXXXXXXXX"
        android:textColor="#000"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="40dp"
        android:orientation="horizontal"
         >
         <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:gravity="center"
             >
            <Button
                android:id="@+id/register_dialog_ok_btn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:background="#0066cc"
                android:gravity="center"
                android:text="确定"
                android:textColor="#fff"/>
         </LinearLayout>
         <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:gravity="center"
             >
            <Button
                android:id="@+id/register_dialog_cancel_btn"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:background="#0066cc"
                android:gravity="center"
                android:text="取消"
                android:textColor="#fff"/>
         </LinearLayout>
    </LinearLayout>
</LinearLayout>
           

2.Java代碼示例:

(這裡隻貼出和對話框有關的代碼,即一個處理自定義Dialog的方法)

//此方法用于處理自定義對話框
        private void processCustomDialog(AlertDialog.Builder builder){
            LayoutInflater inflater=getLayoutInflater();
            final View view=inflater.inflate(R.layout.register_dialog, null);//擷取自定義對話框資源
            //擷取自定義dialog下的确定和取消按鈕資源
            Button okBtnInDialog=(Button)view.findViewById(R.id.register_dialog_ok_btn);
            Button cancelBtnInDialog=(Button)view.findViewById(R.id.register_dialog_cancel_btn);

            builder.setIcon(R.drawable.icon);
            builder.setView(view);
            builder.create();
            builder.show();

            /**
             * 下面為自定義dialog下的确定和取消按鈕設定點選事件
             */
            okBtnInDialog.setOnClickListener(new android.view.View.OnClickListener() {//下面為自定義dialog下的确定按鈕設定點選事件

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent();
                    intent.setClass(Register.this,Vertify.class);
                    startActivity(intent);
                    MyThread thread = new MyThread();
                    thread.start();
                }
            });
            cancelBtnInDialog.setOnClickListener(new android.view.View.OnClickListener() {//下面為自定義dialog下的取消按鈕設定點選事件

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                }
            });

        }
           

值得注意的是:在擷取Dialog上的button等資源時,使用的是view.findViewById(R.id.button)。這才表明你是在對話框View中尋找資源。倘若直接用findViewById(R.id.button),表明在此activity裡找資源ID,能找到才怪!