Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發實作:打造可重複使用的自訂 Dialog

編輯:關於 Android 程式設計

在实际开发中,我们经常遇到这样的场景:不同的界面都需要弹出结构相似的对话框。如果每次都从头写一遍 Dialog 的创建与绑定代码,不仅冗长,还难以维护。本文将介绍一种把 Dialog 封装成可重用组件的设计思路,让标题、内容、按钮与回调都能通过构造函数灵活传入。

第一步:设计布局文件

我们先为 Dialog 设计一份独立的布局。下面这个例子包含标题、内容文字,以及「是 / 否」两个按钮,你可以根据自己的 App 风格自由调整:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/exit_layout"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="@drawable/confirm_dialog_bg2" >

    <TextView
        android:id="@+id/Dlg_Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:padding="5dp"
        android:textColor="#333"
        android:textSize="20sp"
        android:text="標題" />

    <TextView
        android:id="@+id/Dlg_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#333"
        android:layout_marginTop="1dp"
        android:padding="10dp"
        android:textSize="16sp"
        android:gravity="center_horizontal"
        android:text="內容\n" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="33dp"
        android:layout_marginBottom="8dp" >

        <Button
            android:id="@+id/Dlg_Yes"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:text="是"
            android:textSize="16sp"
            android:textColor="#fff"
            android:background="@drawable/btn_style_green"
            android:gravity="center" />

        <Button
            android:id="@+id/Dlg_No"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="否"
            android:textSize="16sp"
            android:textColor="#333"
            android:background="@drawable/btn_style_white"
            android:gravity="center" />
    </LinearLayout>

</LinearLayout>

第二步:封装自定义 Dialog 逻辑

重点来了。设计完布局后,我们需要编写自定义 Dialog 的类。这里最关键的是那个五参数的构造函数与回调接口:通过构造函数,我们在实例化时就把主题样式、标题、内容以及监听器一次性传入。同时自定义一个 CustomDialogListener 接口,当使用者点击「是」或「否」时,Dialog 会回调该接口的 OnClick 方法,随后自动关闭。

public class CustomDialog extends Dialog implements android.view.View.OnClickListener{

    TextView Dlg_title, Dlg_Content;
    Button Dlg_Yes, Dlg_No;
    CustomDialogListener cdListener;
    String title, content;

    public CustomDialog(Context context, int theme, String title, String content, CustomDialogListener cdListener){
        super(context, theme);
        this.cdListener = cdListener;
        this.title = title;
        this.content = content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.exit_dialog);
        this.setCanceledOnTouchOutside(false); // 点击外部不会消失
        InitViews();
    }

    private void InitViews(){
        Dlg_title = (TextView) findViewById(R.id.Dlg_Title);
        Dlg_Content = (TextView) findViewById(R.id.Dlg_content);

        Dlg_title.setText(this.title);
        Dlg_Content.setText(this.content);

        Dlg_Yes = (Button) findViewById(R.id.Dlg_Yes);
        Dlg_No = (Button) findViewById(R.id.Dlg_No);
        Dlg_Yes.setOnClickListener(this);
        Dlg_No.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        cdListener.OnClick(v);
        dismiss();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}

回调接口的定义非常简洁:

public interface CustomDialogListener {
    public void OnClick(View view);
}

第三步:调用自定义 Dialog

最后是调用部分。我们只需在需要弹窗的地方 new 出这个 Dialog,传入标题、内容与一个匿名回调实现,就能根据不同按钮执行相应逻辑:

CustomDialog dialog = new CustomDialog(this, R.style.MyDialog, "退出應用", "請選擇是否退出應用\n", new CustomDialogListener() {

    @Override
    public void OnClick(View view) {
        switch(view.getId()){
            case R.id.Dlg_Yes:
                LoginActivity.this.finish();
                break;
            case R.id.Dlg_No:
                break;
        }
    }
});
dialog.show();

透过这样的封装,日后无论在任何一个 Activity 中需要确认对话框,只要几行代码就能搞定,真正做到「一次编写,处处复用」。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved