1. 程式人生 > >Android 自定義AlertDialog--備忘

Android 自定義AlertDialog--備忘

我們可以通過繼承原生的Dialog來實現自定義的Dialog。

本文的自定義Dialog和原生的AlertDialog的建立方式類似,通過一個靜態Builder類來設定Dialog的圖示、標題、內容和按鈕。

佈局檔案程式碼:

(注意這裡的根佈局的寬高如果用match_parent或者設定為具體的數值都和wrap_conten效果一樣,可以通過設定子控制元件的大小來撐開)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android
="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff"> <LinearLayout android:id="@+id/dialog_header" android:orientation
="vertical" android:layout_width="220dp" android:layout_height="wrap_content" android:padding="16dp" android:gravity="center" android:background="@color/colorGreen" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf
="parent"> <!-- Icon --> <ImageView android:contentDescription="@id/dialog_title" android:id="@+id/dialog_icon" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/ic_check_circle" /> <!-- Title(default is gone) --> <TextView android:id="@+id/dialog_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:textSize="18sp" android:textStyle="bold" android:textColor="#ffffff" android:visibility="gone" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp" android:gravity="center" app:layout_constraintTop_toBottomOf="@+id/dialog_header" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"> <!-- Dialog Message --> <TextView android:id="@+id/dialog_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" tools:text="Dialog Message" /> <Button android:id="@+id/dialog_button" android:layout_width="100dp" android:layout_height="42dp" android:layout_marginTop="16dp" android:layout_marginBottom="8dp" android:background="@drawable/bg_dialog_button" android:textColor="#ffffff" android:text="@string/dialog_button"> </Button> </LinearLayout> </android.support.constraint.ConstraintLayout>

 

package com.cloud.design.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloud.design.R;

public class InfoDialog extends Dialog {

    private InfoDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {

        private View mLayout;

        private ImageView mIcon;
        private TextView mTitle;
        private TextView mMessage;
        private Button mButton;

        private View.OnClickListener mButtonClickListener;

        private InfoDialog mDialog;

        public Builder(Context context) {
            mDialog = new InfoDialog(context, R.style.Theme_AppCompat_Dialog);
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //載入佈局檔案
            mLayout = inflater.inflate(R.layout.dialog, null, false);
            //添加布局檔案到 Dialog
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            mIcon = mLayout.findViewById(R.id.dialog_icon);
            mTitle = mLayout.findViewById(R.id.dialog_title);
            mMessage = mLayout.findViewById(R.id.dialog_message);
            mButton = mLayout.findViewById(R.id.dialog_button);
        }

        /**
         * 通過 ID 設定 Dialog 圖示
         */
        public Builder setIcon(int resId) {
            mIcon.setImageResource(resId);
            return this;
        }

        /**
         * 用 Bitmap 作為 Dialog 圖示
         */
        public Builder setIcon(Bitmap bitmap) {
            mIcon.setImageBitmap(bitmap);
            return this;
        }

        /**
         * 設定 Dialog 標題
         */
        public Builder setTitle(@NonNull String title) {
            mTitle.setText(title);
            mTitle.setVisibility(View.VISIBLE);
            return this;
        }

        /**
         * 設定 Message
         */
        public Builder setMessage(@NonNull String message) {
            mMessage.setText(message);
            return this;
        }

        /**
         * 設定按鈕文字和監聽
         */
        public Builder setButton(@NonNull String text, View.OnClickListener listener) {
            mButton.setText(text);
            mButtonClickListener = listener;
            return this;
        }

        public InfoDialog create() {
            mButton.setOnClickListener(view -> {
                mDialog.dismiss();
                mButtonClickListener.onClick(view);
            });
            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);                //使用者可以點選後退鍵關閉 Dialog
            mDialog.setCanceledOnTouchOutside(false);   //使用者不可以點選外部來關閉 Dialog
            return mDialog;
        }
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button_show_dialog).setOnClickListener(v -> {
            InfoDialog infoDialog = new InfoDialog.Builder(this)
                    .setTitle("Done")
                    .setMessage("Something done")
                    .setButton("OK", view ->
                        Toast.makeText(this, "OK Clicked.", Toast.LENGTH_SHORT).show()
                    ).create();
            infoDialog.show();
        });
    }
}

 

參考:https://www.cnblogs.com/cloudfloating/p/9811380.html