Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 控件封裝:把 Dialog 包成可重複使用的自定義元件

編輯:Android 開發實例

自定義 Dialog 幾乎每個專案都會寫,但每次都寫一遍實在混亂。與其反覆拼湊,不如一次封裝好,以後直接 copy 使用。

主 Activity

public class CustomDialogActivity extends Activity {
    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textView11);
        Button button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDialog myDialog = new MyDialog(CustomDialogActivity.this);
                myDialog.setContent("哥來自Activity");
                myDialog.setDialogCallback(dialogcallback);
                myDialog.show();
            }
        });
    }

    // 定義 Dialog 需要回傳給 Activity 處理的事情
    Dialogcallback dialogcallback = new Dialogcallback() {
        @Override
        public void dialogdo(String string) {
            textView.setText("哥來自Dialog: " + string);
        }
    };
}

核心:MyDialog

public class MyDialog {
    Context context;
    Dialogcallback dialogcallback;
    Dialog dialog;
    Button sure;
    TextView textView;
    EditText editText;

    public MyDialog(Context con) {
        this.context = con;
        dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(R.layout.dialog);

        textView = (TextView) dialog.findViewById(R.id.textview);
        sure = (Button) dialog.findViewById(R.id.button1);
        editText = (EditText) dialog.findViewById(R.id.editText1);

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogcallback.dialogdo(editText.getText().toString());
                dismiss();
            }
        });
    }

    // 透過介面讓 Dialog 能呼叫 Activity 定義的行為
    public interface Dialogcallback {
        public void dialogdo(String string);
    }

    public void setDialogCallback(Dialogcallback dialogcallback) {
        this.dialogcallback = dialogcallback;
    }

    public void setContent(String content) {
        textView.setText(content);
    }

    public String getText() {
        return editText.getText().toString();
    }

    public void show()    { dialog.show(); }
    public void hide()    { dialog.hide(); }
    public void dismiss() { dialog.dismiss(); }
}

這裡的關鍵設計是 Dialogcallback 介面:Dialog 只負責收集輸入並「喊一聲」,實際要拿這筆資料做什麼,交由外部的 Activity 決定。這樣元件就跟具體業務解耦了。

Dialog 的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="30.0dp"
    android:orientation="vertical"
    android:padding="10dip">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30.0dp"
        android:gravity="center"
        android:text="自定義Dialog"
        android:textColor="#F0F"
        android:textSize="20dip" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="確定" />
</LinearLayout>

Style 設定

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">#FFF</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>
</resources>

這份 style 做了三件事:拿掉標題列與邊框、讓背景透明、並開啟背景變暗(backgroundDimEnabled),這樣彈窗浮在頁面上時才有層次感。

使用方式

之後任何頁面要用,只要三行:建立 MyDialog、setContent() 設定文字、setDialogCallback() 掛上回呼,然後 show()。要改版面就改 dialog.xml,要改外觀就改 style,不必再重寫一遍流程。

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