分析:
《1》加載根布局 onCreate中setContentView
《2》使用接口回調(diào)dialog和activity的傳值,當(dāng)點(diǎn)擊確定的時(shí)候惠勒,回調(diào)姚建;
當(dāng)點(diǎn)擊取消的時(shí)候,只關(guān)閉就好
//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="被保人為個(gè)人"
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; //確認(rèn)按鈕
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());
//初始化監(jiān)聽
initListener();
}
private void initListener() {
//設(shè)置確定按鈕被點(diǎn)擊后烹棉,向外界提供監(jiān)聽
tvConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onConfirmListener != null) {
onConfirmListener.onConfirmClick();
if (mDialogCallBackListener != null){
//===========最重要的回調(diào)===============
mDialogCallBackListener.callBack(text);
}
}
}
});
//設(shè)置取消按鈕被點(diǎn)擊后,向外界提供監(jiān)聽
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) {
//獲取變更后的選中項(xiàng)的id
int checkedRadioButtonId = group.getCheckedRadioButtonId();
//根據(jù)id獲取RadioButton的實(shí)例
RadioButton rb = (RadioButton) findViewById(checkedRadioButtonId);
//獲取文本
text = rb.getText();
}
});
}
private void init(Context context) {
setCancelable(true); //設(shè)置不可取消怯疤,點(diǎn)擊其他區(qū)域不能取消浆洗,
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;//確定按鈕被點(diǎn)擊了的監(jiān)聽器
private CancelListener onCancelListener;//取消按鈕被點(diǎn)擊了的監(jiān)聽器
/**
* 設(shè)置確定按鈕和取消被點(diǎn)擊的接口
*/
public interface ConfirmListener {
void onConfirmClick();
}
public interface CancelListener {
void onCancelClick();
}
/**
* 設(shè)置取消按鈕的顯示內(nèi)容和監(jiān)聽
* @param onCancelListener
*/
public void setCancelListener(CancelListener onCancelListener) {
this.onCancelListener = onCancelListener;
}
/**
* 設(shè)置確定按鈕的顯示內(nèi)容和監(jiān)聽
* @param onConfirmListener
*/
public void setConfirmListener(ConfirmListener onConfirmListener) {
this.onConfirmListener = onConfirmListener;
}
public interface DialogCallBackListener{//通過該接口回調(diào)Dialog需要傳遞的值
void callBack(CharSequence text);//具體方法
}
//設(shè)置RradioGroup的回調(diào)
private DialogCallBackListener mDialogCallBackListener;
/**
* 設(shè)置RradioGroup的回調(diào)
* @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="輸入內(nèi)容"
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(一)實(shí)現(xiàn)文字驗(yàn)證碼
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的詳細(xì)步驟(實(shí)現(xiàn)自定義樣式一般原理)
http://blog.csdn.net/oqihaogongyuan/article/details/50958659