【举例】Android自定义Dialog——选择一个RadioButton
来源:互联网 发布:免费数据库 编辑:程序博客网 时间:2024/06/03 20:09
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
欢迎大家补充交流。
0 0
- 【举例】Android自定义Dialog——选择一个RadioButton
- 【举例】Android自定义Dialog——做出“确定/取消”的选择
- android自定义一个dialog
- Android Dialog使用举例
- Android Dialog使用举例
- Android Dialog 使用举例
- Android Dialog使用举例
- RadioButton - android 自定义RadioButton样式
- android中自定义radiobutton的背景色选择默认图标
- android dialog——自定义对话框之一
- Android学习——自定义Dialog
- 自定义Dialog android Dialog
- android 自定义RadioButton样式
- Android RadioButton的自定义
- android 自定义RadioButton样式
- Android 自定义RadioButton样式
- Android自定义RadioButton
- Android自定义RadioButton样式
- 操作jQuery集合
- (一)分布式存储综述
- 经纬度坐标映射到平面直角坐标系
- 简单的登录缓冲进程的实现
- qtcreator配置opencv找不到头文件的问题
- 【举例】Android自定义Dialog——选择一个RadioButton
- Calabash Android安装
- C++ 初始化类的常量数据成员、静态数据成员、常量静态数据成员
- J2EE中常用的名词解释
- android viewpager非常实用的广告轮播图
- java个人笔记之UDP和TCP
- Android获取图片、裁剪、压缩、上传头像
- Error:Execution failed for task ':app:transformClassesWithInstantRunForDebug'.错误
- 16_python_练习题——使用webdriver获取当前页面截屏以及滑动页面