Android 自定義Dialog類,並在Activity中實現按鈕監聽。

 

實際開發中,常常會用到Dialog,好比退出時候會彈出是否退出,或者還有一些編輯框也會用Dialog實現,效果圖以下:android

開發中遇到的問題無非在於若是在Activity中監聽這個Dialog中實現的按鈕,Dialog類以下,在MyDialog這個類中實現了一個LeaveMyDialogListener接口,用來實現onclick的點擊事件:app

package com.Ieasy.Tool;
import com.Ieasy.ieasyware.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog implements android.view.View.OnClickListener {

    private Context context;
    private TextView txt;
    private Button   btnok,btnedit,btncancle,btnsave;
    private LeaveMyDialogListener listener;
    
    public interface LeaveMyDialogListener{   
        public void onClick(View view);   
    }   
    
    public MyDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
    }
    
    public MyDialog(Context context,int theme,LeaveMyDialogListener listener) {
        super(context,theme);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(com.Ieasy.ieasyware.R.layout.mydialog);
        btncancle = (Button)findViewById(R.id.mycancle);
        btnedit   = (Button)findViewById(R.id.myedit);
        btnok     = (Button)findViewById(R.id.myok);
        txt       = (TextView)findViewById(R.id.miaosu);
        btnsave   = (Button)findViewById(R.id.mysave);
        btncancle.setOnClickListener(this);
        btnedit.setOnClickListener(this);
        btnok.setOnClickListener(this);
        btnsave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        listener.onClick(v);
    }
}

佈局文件以下:ide

<?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" >

    <LinearLayout
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/night_biz_subscribe_media_recommend_item_bg"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/miaosu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:text="描述"
            android:textColor="@color/whitesmoke"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/myok"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btnclick"
                android:textColor="@color/whitesmoke"
                android:text="肯定" />

            <Button
                android:id="@+id/myedit"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btnclick"
                android:textColor="@color/whitesmoke"
                android:text="編輯" />
            
            <Button
                android:id="@+id/mysave"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btnclick"
                android:textColor="@color/whitesmoke"
                android:text="保存" />

            <Button
                android:id="@+id/mycancle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/btnclick"
                android:textColor="@color/whitesmoke"
                android:text="取消" />
            
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
View Code

引用的style:佈局

 <style name="MyDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item> 
        <item name="android:windowBackground">@drawable/night_biz_subscribe_media_recommend_item_bg</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

最後在Activity中調用,經過LeaveMyDialogListener 接口來實如今Activity中的點擊事件this


MyDialog dialog = new MyDialog(context,R.style.MyDialog, new MyDialog.LeaveMyDialogListener() { @Override public void onClick(View view) { switch(view.getId()){ case R.id.myok: break; case R.id.myedit:
break; case R.id.mycancle: break; case R.id.mysave: dialog.dismiss(); default: break; } } }); dialog.show();

若是想得到Dialog中的TextView控件能夠這樣獲取,給TextView賦值時候必定要在Dialog show了以後在賦值,你懂得。spa

TextView text = (TextView) dialog.findViewById(R.id.miaosu);
相關文章
相關標籤/搜索