玩转自定义dialog

分析:
《1》加载根布局 onCreate中setContentView
《2》使用接口回调dialog和activity的传值,当点击确定的时候,回调;
当点击取消的时候,只关闭就好
//dialog的背景选择器
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="@android:color/white"/>
</shape>
//RadioButton的选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/bg_1"/>
<item android:state_checked="false" android:drawable="@drawable/bg_2"/>
</selector>
//自定义dialog的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/bg_selector"
android:paddingBottom="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:textColor="@color/text_dark"
android:text="请选择被保人类型"
android:textSize="15dp"/>
<RadioGroup
android:layout_marginTop="20dp"
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb_company"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="被保人为公司"
android:button="@null"
android:drawableRight="@drawable/shop_seleted"/>
<RadioButton
android:id="@+id/rb_personal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="被保人为个人"
android:button="@null"
android:drawableRight="@drawable/shop_seleted"/>
</RadioGroup>
<View
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray"/>
<LinearLayout
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_cancel"
android:gravity="center"
android:text="取消"
android:layout_weight="1"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_confirm"
android:gravity="center"
android:text="确定"
android:textSize="18dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
//自定义dialog
public class CustomDialog extends AlertDialog{
TextView tvConfirm; //确认按钮
TextView tvCancel; //取消按钮
RadioGroup radioGroup;
CharSequence text;
private LayoutInflater mLayoutInflater;
public CustomDialog(Context context) {
super(context);
}
public CustomDialog(Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayoutInflater = LayoutInflater.from(getContext());
init(getContext());
//初始化监听
initListener();
}
private void initListener() {
//设置确定按钮被点击后,向外界提供监听
tvConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onConfirmListener != null) {
onConfirmListener.onConfirmClick();
if (mDialogCallBackListener != null){
//===========最重要的回调===============
mDialogCallBackListener.callBack(text);
}
}
}
});
//设置取消按钮被点击后,向外界提供监听
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onCancelListener != null) {
onCancelListener.onCancelClick();
}
}
});

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            //获取变更后的选中项的id
            int checkedRadioButtonId = group.getCheckedRadioButtonId();
            //根据id获取RadioButton的实例
            RadioButton rb = (RadioButton) findViewById(checkedRadioButtonId);
            //获取文本
            text = rb.getText();
        }
    });
}

private void init(Context context) {
    setCancelable(true); //设置不可取消,点击其他区域不能取消,
    setCanceledOnTouchOutside(false);
   //===========加载根布局============
    View contentView = mLayoutInflater.inflate(R.layout.custom_dialog,null);
    setContentView(contentView);
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.width = WindowManager.LayoutParams.MATCH_PARENT;
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    getWindow().setAttributes(params);
    //初始化view
    radioGroup = (RadioGroup) contentView.findViewById(R.id.radio_group);
    tvCancel = (TextView) contentView.findViewById(R.id.tv_cancel);
    tvConfirm = (TextView) contentView.findViewById(R.id.tv_confirm);
}
private ConfirmListener onConfirmListener;//确定按钮被点击了的监听器
private CancelListener onCancelListener;//取消按钮被点击了的监听器
/**
 * 设置确定按钮和取消被点击的接口
 */
public interface ConfirmListener {
    void onConfirmClick();
}
public interface CancelListener {
    void onCancelClick();
}
/**
 * 设置取消按钮的显示内容和监听
 * @param onCancelListener
 */
public void setCancelListener(CancelListener onCancelListener) {
    this.onCancelListener = onCancelListener;
}
/**
 * 设置确定按钮的显示内容和监听
 * @param onConfirmListener
 */
public void setConfirmListener(ConfirmListener onConfirmListener) {
    this.onConfirmListener = onConfirmListener;
}
public interface DialogCallBackListener{//通过该接口回调Dialog需要传递的值
    void callBack(CharSequence text);//具体方法
}
//设置RradioGroup的回调
private DialogCallBackListener mDialogCallBackListener;
/**
 * 设置RradioGroup的回调
 * @param mDialogCallBackListener
 */
public void setDialogCallBackListener(DialogCallBackListener mDialogCallBackListener) {
    this.mDialogCallBackListener = mDialogCallBackListener;
}

}
//显示dialog
//activity的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lenvo.testdemo.SecondActivity">
<Button
android:id="@+id/show_dialog"
android:text="弹出dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview"
android:text="输入内容"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

public class SecondActivity extends AppCompatActivity {
CustomDialog customDialog;
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = (Button) findViewById(R.id.show_dialog);
textView = (TextView) findViewById(R.id.textview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show();
}
});
}
private void show() {
customDialog = new CustomDialog(SecondActivity.this);
customDialog.setConfirmListener(new CustomDialog.ConfirmListener() {
@Override
public void onConfirmClick() {
Toast.makeText(SecondActivity.this, "确定", Toast.LENGTH_SHORT).show();
customDialog.dismiss();

        }
    });
    customDialog.setCancelListener(new CustomDialog.CancelListener() {
        @Override
        public void onCancelClick() {
            Toast.makeText(SecondActivity.this, "取消", Toast.LENGTH_SHORT).show();
            customDialog.dismiss();
        }
    });
    customDialog.setDialogCallBackListener(new CustomDialog.DialogCallBackListener() {
        @Override
        public void callBack(CharSequence text) {
            textView.setText(text);
            Log.e("文本",text.toString());
        }
    });
    customDialog.show();
}

}

链接参考:
Android自定义View(一)实现文字验证码
http://blog.csdn.net/xuyonghong1122/article/details/51288795
自定义Dialog(二)之Dialog与Activity传值
http://blog.csdn.net/xuyonghong1122/article/details/51074474
Android控件系列之RadioButton&RadioGroup
http://www.cnblogs.com/wt616/archive/2011/06/20/2085531.html
自定义Dialog的详细步骤(实现自定义样式一般原理)
http://blog.csdn.net/oqihaogongyuan/article/details/50958659

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容