Android實現(xiàn)類似iOS風(fēng)格的對話框?qū)嵗a
分享一個簡單的常用的對話框類,按照國際慣例,先上圖

布局簡單,先上布局。一個標(biāo)題,一個內(nèi)容,兩個按鈕
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/shape_diglog_bg"
android:orientation="vertical">
<TextView
android:id="@+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="這里是標(biāo)題"
android:textColor="#333333"
android:textSize="19sp"
android:visibility="visible" />
<TextView
android:id="@+id/dialog_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:text="這里是內(nèi)容"
android:textColor="#333333"
android:textSize="17sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="18sp"
android:background="#f1f1f1" />
<LinearLayout
android:id="@+id/ll_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:text="取消"
android:textColor="#006DFF"
android:textSize="17sp" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#f1f1f1" />
<Button
android:id="@+id/dialog_ensure"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:text="確定"
android:textColor="#006DFF"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
接著就是自定義類!
首先,新建類繼承Dialog
public class CommonDialog extends Dialog
接著是構(gòu)造函數(shù),在構(gòu)造函數(shù)中定義樣式
public CommonDialog(@NonNull Context context) {
super(context, R.style.dialog_Common);
mContext = context;
setContentView(R.layout.dialog_common);
ButterKnife.bind(this);
}
其中,在style中定義對話框?qū)傩?/p>
<style name="dialog_Common" parent="android:style/Theme.Dialog"> <!--說明提示框是否有邊框--> <item name="android:windowFrame">@null</item> <!--說明提示框是否有標(biāo)題--> <item name="android:windowNoTitle">true</item> <!--設(shè)置提示框的背景顏色是什么--> <item name="android:windowBackground">@android:color/transparent</item> <!--是否浮現(xiàn)在activity之上--> <item name="android:windowIsFloating">true</item> <!--是否有遮蓋--> <item name="android:windowContentOverlay">@null</item> <!--說明提示框是滯是透明的--> <item name="android:windowIsTranslucent">true</item> <!--說明是否充許對話框的背景變暗。為true則充許變暗--> <item name="android:backgroundDimEnabled">true</item> <!--設(shè)置背景透明度--> <item name="android:backgroundDimAmount">0.4</item> </style>
緊接著,提供四個變量來存儲設(shè)置的標(biāo)題、內(nèi)容以及兩個按鈕的文字
private String title; private String content; private String cancel; private String ensure;
現(xiàn)在需要提供能夠設(shè)置標(biāo)題、內(nèi)容以及兩個按鈕的文字的方法
/*
* 設(shè)置標(biāo)題 默認沒有標(biāo)題
*
* @param title
*/
public void setTitle(String title) {
this.title = title;
}
/**
* 設(shè)置內(nèi)容 默認為空
*
* @param content
*/
public void setContent(String content) {
this.content = content;
}
/**
* 設(shè)置確定按鈕內(nèi)容 默認為確定
*
* @param ensure
*/
public void setEnsure(String ensure) {
this.ensure = ensure;
}
/**
* 設(shè)置取消按鈕內(nèi)容 默認為取消
*
* @param cancel
*/
public void setCancel(String cancel) {
this.cancel = cancel;
}
現(xiàn)在,處理按鈕的點擊事件
/**
* 確定按鈕事件監(jiān)聽 默認是dismiss對話框
*
* @param onEnsureClickListener
*/
public void setOnEnsureClickListener(View.OnClickListener onEnsureClickListener) {
this.onEnsureClickListener = onEnsureClickListener;
}
/**
* 取消按鈕事件監(jiān)聽 默認是dismiss對話框
*
* @param onCabcelClickListener
*/
public void setOnCancelClickListener(View.OnClickListener onCabcelClickListener) {
this.onCancelClickListener = onCabcelClickListener;
}
默認的是點擊對話框消失
/**
* 默認點擊事件,點擊彈框消失
*/
private View.OnClickListener onClickListenerDismiss = new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
};
最后,重寫父類的show方法,將展示之前設(shè)置的各種信息
/**
* 重寫show方法
*/
@Override
public void show() {
if (TextUtils.isEmpty(title)) {
//默認沒有標(biāo)題
dialogTitle.setVisibility(View.GONE);
} else {
//默認不設(shè)置內(nèi)容,則內(nèi)容太為空
dialogTitle.setVisibility(View.VISIBLE);
setTextViewTxt(dialogTitle, title);
}
if (TextUtils.isEmpty(cancel)) {
//默認取消按鈕文字為"取消"
cancel = mContext.getString(R.string.cancel);
}
if (TextUtils.isEmpty(ensure)) {
//默認確認按鈕文字為"確認"
ensure = mContext.getString(R.string.ensure);
}
//設(shè)置文字信息
setTextViewTxt(dialogContent, content);
setTextViewTxt(dialogCancel, cancel);
setTextViewTxt(dialogEnsure, ensure);
//設(shè)置點擊事件
setButtonOnClickListener(dialogCancel, onCancelClickListener);
setButtonOnClickListener(dialogEnsure, onEnsureClickListener);
super.show();
}
最最后,獻上在Activity中如何使用該對話框的方法的代碼
public class MainActivity extends AppCompatActivity {
@BindView(R.id.btn_demo_haveTitle)
Button btnDemoHaveTitle;
@BindView(R.id.btn_demo_noTitle)
Button btnDemoNoTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick({R.id.btn_demo_haveTitle, R.id.btn_demo_noTitle})
public void onClick(View view) {
switch (view.getId()) {
//沒有標(biāo)題的對話框
case R.id.btn_demo_haveTitle:
final CommonDialog dialog1 = new CommonDialog(this);
dialog1.setTitle("提示");
dialog1.setContent("是否確認退出?");
dialog1.setOnEnsureClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"點擊了確認",Toast.LENGTH_SHORT).show();
// TODO: 2017/9/17 這里寫你的代碼
dialog1.dismiss();
}
});
dialog1.show();
break;
//有標(biāo)題的對話框
case R.id.btn_demo_noTitle:
final CommonDialog dialog2 = new CommonDialog(this);
//不設(shè)置標(biāo)題默認沒有標(biāo)題
dialog2.setContent("是否確認退出?");
dialog2.setOnEnsureClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"點擊了確認",Toast.LENGTH_SHORT).show();
// TODO: 2017/9/17 這里寫你的代碼
dialog2.dismiss();
}
});
dialog2.show();
break;
}
}
}
總結(jié)
以上所述是小編給大家介紹的Android實現(xiàn)類似iOS風(fēng)格的對話框樣式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)
- Android自定義PopupWindow仿點擊彈出分享功能
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android自定義PopupWindow簡單小例子
- Android自定義彈出窗口PopupWindow使用技巧
- Android中自定義PopupWindow實現(xiàn)彈出框并帶有動畫效果
- Android仿IOS底部彈出對話框
- android底部彈出iOS7風(fēng)格對話選項框(QQ對話框)--第三方開源之IOS_Dialog_Library
- Android自定義PopupWindow實現(xiàn)炫酷的IOS對話框效果
相關(guān)文章
android實現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫代碼
這篇文章主要介紹了android實現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫代碼,有需要的朋友可以參考一下2013-12-12
Android 實例開發(fā)一個學(xué)生管理系統(tǒng)流程詳解
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實現(xiàn)一個學(xué)生管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
Android:利用SharedPreferences實現(xiàn)自動登錄
本篇文章主要介紹了Android實現(xiàn)自動登錄,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
Android RecyclerView的Item點擊事件實現(xiàn)整理
這篇文章主要介紹了Android RecyclerView的Item點擊事件實現(xiàn)整理的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android中檢查網(wǎng)絡(luò)連接狀態(tài)的變化無網(wǎng)絡(luò)時跳轉(zhuǎn)到設(shè)置界面
這篇文章主要介紹了Android中檢查網(wǎng)絡(luò)連接狀態(tài)的變化無網(wǎng)絡(luò)時跳轉(zhuǎn)到設(shè)置界面,需要的朋友可以參考下2017-06-06

