1. 程式人生 > >【舉例】Android自定義Dialog——選擇一個RadioButton

【舉例】Android自定義Dialog——選擇一個RadioButton

1. 自定義Dialog的介面


<--!dialog_selectserver.xml-->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background
="@drawable/mydiabg" android:orientation="vertical">
<TextView android:id="@+id/groupnameet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text
="選擇伺服器\n" android:textColor="@color/blue" android:textSize="25sp" />
<View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="10dp" android:background="@color/lightgrey" android:padding="20dp" />
<RadioGroup android:id="@+id/selectserver_radiogrp" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <RadioButton android:id="@+id/labserver_radiobtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="實驗室伺服器" android:textColor="@color/black" android:textSize="18dp"> </RadioButton> <RadioButton android:id="@+id/aliyunserver_radiobtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="阿里雲伺服器" android:textColor="@color/black" android:textSize="18dp"></RadioButton> </RadioGroup> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="\n" /> </LinearLayout>

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.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;


/**
 * Created by guan on 3/13/17.
 */

public class ServerSettingDialog extends Dialog {

    RadioGroup selectserver_radiogrp;
    DialogCallback callback;

    //這裡定義了一些關於該Dialog的一些選項
    public enum SelectedServer {Lab, Aliyun}

    //建構函式
    public ServerSettingDialog(Context context) {
        super(context, R.style.Dialog_Dim);
        setCustomeDialog();

        //==========對Dialog窗體大小的設定========
        //設定dialog的寬度
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (AppConstant.SCRRENWIDTH * 0.8); //設定寬度
        getWindow().setAttributes(lp);
        //getWindow().setLayout((int) (AppConstant.SCRRENWIDTH * 0.8), (int) (AppConstant.SCREENHEIGHT * 0.3));
    }
    //建構函式
    public ServerSettingDialog(Context context, int themeResId) {
        super(context, themeResId);
        setCustomeDialog();
        //getWindow().setLayout((int) (AppConstant.SCRRENWIDTH * 0.8), (int) (AppConstant.SCREENHEIGHT * 0.3));
    }

    //建構函式
    protected ServerSettingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        setCustomeDialog();
        //getWindow().setLayout((int) (AppConstant.SCRRENWIDTH * 0.8), (int) (AppConstant.SCREENHEIGHT * 0.3));
    }


    public void setDialogCallback(DialogCallback callback) {
        this.callback = callback;
    }

    //具體設定該Dialog
    private void setCustomeDialog() {
        View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_selectserver, null);
        selectserver_radiogrp = (RadioGroup) mView.findViewById(R.id.selectserver_radiogrp);

        //這一部分程式碼表示具體要做的事.....
        selectserver_radiogrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.labserver_radiobtn:
                        callback.onClickRadioButton(SelectedServer.Lab);
                        break;
                    case R.id.aliyunserver_radiobtn:
                        callback.onClickRadioButton(SelectedServer.Aliyun);
                        break;
                }
                ServerSettingDialog.this.dismiss();
            }
        });

        super.setContentView(mView);

    }

    //自定義的,關於該Dialog的回撥介面
    public interface DialogCallback {

        void onClickRadioButton(SelectedServer s);

    }

}

4. 使用Dialog

                ServerSettingDialog dialog = new ServerSettingDialog(mContext);
                dialog.setCanceledOnTouchOutside(true);//觸控dialog以外的區域,dialog會消失
                //設定Dialog的回撥函式
                dialog.setDialogCallback(new ServerSettingDialog.DialogCallback() {
                    //根據不同的選擇,做出不同的動作
                    @Override
                    public void onClickRadioButton(ServerSettingDialog.SelectedServer s) {
                        if (s == ServerSettingDialog.SelectedServer.Lab) {
                            //balabala
                        } else if (s == ServerSettingDialog.SelectedServer.Aliyun) {
                            //balabala
                        }

                    }
                });
                //新增Dialog消失時的listener
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        showSelectedServerOnActivity();
                    }
                });
                //顯示該Dialog,不要忘記
                dialog.show();

抓住Demo一隻:

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

https://github.com/shanxu100/GUtils

歡迎大家補充交流。