1. 程式人生 > >【舉例】Android自定義Dialog——做出“確定/取消”的選擇

【舉例】Android自定義Dialog——做出“確定/取消”的選擇

1、自定義Dialog的介面

<--!dialog_yesorno_for_permission.xml-->

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootView"
    android:layout_width="250dp"
    android:layout_height="170dp"
    android:background
="@drawable/diabg_bkg" android:orientation="vertical" >
<RelativeLayout android:id="@+id/top_rl" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/bottomo_ll" > <TextView android:id
="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:gravity="center_horizontal|center_vertical" android:text="請授予相關許可權,否則程式無法執行。\n\n點選確定。\n點選取消。\n"
android:textColor="@color/black" />
</RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@+id/top_rl" android:layout_marginBottom="10dp" android:background="@color/lightgrey" /> <RelativeLayout android:id="@+id/bottomo_ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/acceptbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:layout_weight="1" android:background="@null" android:text="確定" android:textColor="@color/black" android:textSize="18sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/lightgrey" /> <Button android:id="@+id/refusebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="6dp" android:layout_marginTop="12dp" android:layout_weight="1" android:background="@null" android:text="取消" android:textColor="@color/black" android:textSize="18sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>

2. Dialog的形狀背景等設定

<--!diabg_bkg.xml-->

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <corners android:radius="16dp" />

</shape>

3.關於該Dialog的Java類檔案


import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


public class YesOrNoDialog extends Dialog {
    private Button positiveButton, negativeButton;
    private TextView message;
    private YesOrNoDialogCallback callback;

    //定義“確定/取消”的操作
    public enum ClickedButton {POSITIVE, NEGATIVE}

    public YesOrNoDialog(Context context) {
        super(context, R.style.Dialog_Dim);
        setCustomDialog();
        getWindow().setLayout((int) (AppConstant.SCRRENWIDTH * 0.7), (int) (AppConstant.SCREENHEIGHT * 0.3));
    }

    public void setCallback(YesOrNoDialogCallback callback) {
        this.callback = callback;
    }

    //具體的設定Dialog
    private void setCustomDialog() {
        View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_yesorno_for_permission, null);
        message = (TextView) mView.findViewById(R.id.title);
        positiveButton = (Button) mView.findViewById(R.id.acceptbtn);
        negativeButton = (Button) mView.findViewById(R.id.refusebtn);
        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onClickButton(ClickedButton.POSITIVE,"");
            }
        });
        negativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onClickButton(ClickedButton.NEGATIVE,"");
            }
        });

        //ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        super.setContentView(mView);
    }

    @Override
    public void setContentView(int layoutResID) {
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
    }

    @Override
    public void setContentView(View view) {
    }


    public void setMeesage(String mes) {
        message.setText(mes);
    }

    //定義回撥的介面
    public interface YesOrNoDialogCallback {
        public void onClickButton(ClickedButton button,String message);
    }

}

4.使用Dialog

        YesOrNoDialog ynDialog = new YesOrNoDialog(mContext);

        ynDialog.setMeesage("請授予相關許可權,否則程式無法執行。\n\n點選確定,重新授予許可權。\n點選取消,立即終止程式。\n");
        ynDialog.setCallback(new YesOrNoDialog.YesOrNoDialogCallback() {
            @Override
            public void onClickButton(YesOrNoDialog.ClickedButton button, String message) {
                if (button == YesOrNoDialog.ClickedButton.POSITIVE) {
                    ynDialog.dismiss();
                    ....
                    //balabala

                } else if (button == YesOrNoDialog.ClickedButton.NEGATIVE) {
                    ynDialog.dismiss();
                    ....
                    //balabala
                }
            }
        });

        //顯示該Dialog,不要忘記
        dialog.show();

抓住Demo一隻:

為了方便以後呼叫,本人已經將上述Dialog封裝到了.aar檔案中。以後呼叫時,只需要匯入第三方包即可。

https://github.com/shanxu100/GUtils

歡迎大家補充交流。