分析:
《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
网友评论