自定義按鈕的實(shí)現(xiàn),很多自定義的控件都可以以類(lèi)似的方式實(shí)現(xiàn)。比較簡(jiǎn)單
public class SampleDialog extends Dialog {
@BindView(R.id.content)
TextView contextTxt;
@BindView(R.id.title)
TextView titleTxt;
@BindView(R.id.submit)
TextView submitTxt;
@BindView(R.id.cancel)
TextView cancelTxt;
private Context mContext;
private String content;//提示內(nèi)容
private OnCloseListener listener;//按鈕監(jiān)聽(tīng)
private String positiveName;//確認(rèn)文本
private String negativeName;//取消文本
private String title;//標(biāo)題
//Dialog本身的構(gòu)造函數(shù)就不添加了,實(shí)現(xiàn)的時(shí)候需補(bǔ)上驮宴,這里放上需要的構(gòu)造函數(shù)
public SampleDialog(Context context, int themeResId, String content, OnCloseListener listener) {
super(context, themeResId);
this.mContext = context;
this.content = content;
this.listener = listener;
}
//設(shè)置標(biāo)題
public SampleDialog setTitle(String title){
this.title = title;
return this;
}
//設(shè)置確認(rèn)文本
public SampleDialog setPositiveButton(String name){
this.positiveName = name;
return this;
}
//設(shè)置取消文本
public SampleDialog setNegativeButton(String name){
this.negativeName = name;
return this;
}
//設(shè)置確認(rèn)點(diǎn)擊事件
@OnClick(R.id.submit)
void clickSubmit(){
if(listener != null){
listener.onClick(this, true);
}
this.dismiss();
}
//設(shè)置確認(rèn)取消點(diǎn)擊事件
@OnClick(R.id.cancel)
void clickCancel(){
if(listener != null){
listener.onClick(this,false);
}
this.dismiss();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_commom);//設(shè)置dialog,具體的樣式請(qǐng)隨意發(fā)揮呕缭,只需保證有4個(gè)控件堵泽,標(biāo)題,內(nèi)容文本恢总,確認(rèn)按鈕迎罗,取消按鈕
setCanceledOnTouchOutside(false);
ButterKnife.bind(this);
initView();
}
//初始化文本
private void initView(){
contextTxt.setText(content);
if(!TextUtils.isEmpty(positiveName)){
submitTxt.setText(positiveName);
}
if(!TextUtils.isEmpty(negativeName)){
cancelTxt.setText(negativeName);
}
if(!TextUtils.isEmpty(title)){
titleTxt.setText(title);
}
}
//接口,需在實(shí)際使用中實(shí)現(xiàn)
public interface OnCloseListener{
void onClick(Dialog dialog, boolean confirm);
}
}
實(shí)現(xiàn)的例子
SampleDialog dialog2 = new SampleDialog(xxxActivity.this,R.style.dialog, "hello world", new SampleDialog.OnCloseListener() {
@Override
public void onClick(Dialog dialog, boolean confirm) {
if(confirm){
//DO YES
}else{
//DO NO
dialog.dismiss();//關(guān)閉dialog
}
}
});
dialog2.setPositiveButton("是!");
dialog2.setNegativeButton("我點(diǎn)錯(cuò)啦片仿!");
dialog2.show();
補(bǔ)充兩個(gè)內(nèi)容纹安,dialog的THEME,以及 animation 效果
values/styles.xml
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!--邊框-->
<item name="android:windowIsFloating">true</item>
<!--是否浮現(xiàn)在activity之上-->
<item name="android:windowIsTranslucent">false</item>
<!--半透明-->
<item name="android:windowNoTitle">true</item>
<!--無(wú)標(biāo)題-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--背景透明-->
<item name="android:backgroundDimEnabled">true</item>
<!--模糊-->
</style>
<style name="dialogWindowAnim" mce_bogus="1" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/dialog_enter_anim</item>
<item name="android:windowExitAnimation">@anim/dialog_exit_anim</item>
</style>
動(dòng)畫(huà)
動(dòng)畫(huà):1 anim/dialog_enter_anim
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
動(dòng)畫(huà):2 anim/dialog_exit_anim
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="200"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>