玩轉(zhuǎn)自定義dialog

分析:
《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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市集峦,隨后出現(xiàn)的幾起案子伏社,更是在濱河造成了極大的恐慌,老刑警劉巖塔淤,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摘昌,死亡現(xiàn)場離奇詭異,居然都是意外死亡高蜂,警方通過查閱死者的電腦和手機(jī)聪黎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來备恤,“玉大人挺举,你說我怎么就攤上這事『娑澹” “怎么了湘纵?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長滤淳。 經(jīng)常有香客問我梧喷,道長,這世上最難降的妖魔是什么脖咐? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任铺敌,我火速辦了婚禮,結(jié)果婚禮上屁擅,老公的妹妹穿的比我還像新娘偿凭。我一直安慰自己,他們只是感情好派歌,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布弯囊。 她就那樣靜靜地躺著痰哨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪匾嘱。 梳的紋絲不亂的頭發(fā)上斤斧,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機(jī)與錄音霎烙,去河邊找鬼撬讽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛悬垃,可吹牛的內(nèi)容都是我干的游昼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼尝蠕,長吁一口氣:“原來是場噩夢啊……” “哼酱床!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起趟佃,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎昧捷,沒想到半個(gè)月后闲昭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡靡挥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年序矩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片跋破。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡簸淀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出毒返,到底是詐尸還是另有隱情租幕,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布拧簸,位于F島的核電站劲绪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏盆赤。R本人自食惡果不足惜贾富,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望牺六。 院中可真熱鬧颤枪,春花似錦、人聲如沸淑际。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至霍骄,卻和暖如春台囱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背读整。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工簿训, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人米间。 一個(gè)月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓强品,卻偏偏與公主長得像,于是被迫代替她去往敵國和親屈糊。 傳聞我的和親對象是個(gè)殘疾皇子的榛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容