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文件中。以后调用时,只需要导入第三方包即可。
欢迎大家补充交流。