安卓系統(tǒng)的提供的dialog往往不能滿足實(shí)際項(xiàng)目要求,此時(shí)就需要根據(jù)需求實(shí)現(xiàn)自定view醉途。通常dialog作為一種提示性對(duì)話框來(lái)使用洒缀,不需要從外界獲取數(shù)據(jù)衷佃,然后展示在dialog上,就像退出時(shí)提示框又或者額是登陸時(shí)發(fā)生錯(cuò)誤贮庞,進(jìn)行簡(jiǎn)單的登錄異常信息提示峦筒;而另外一種用途通常需要從外界獲取數(shù)據(jù)作為dialog的view來(lái)進(jìn)行展示,此時(shí)就需要自定義實(shí)現(xiàn)窗慎,點(diǎn)擊某個(gè)數(shù)據(jù)項(xiàng)之后物喷,觸發(fā)相關(guān)操作;
自定義dialog思路解析:
1dialog的界面布局遮斥;
2外界傳入的數(shù)據(jù)峦失;(有些dialog不需要數(shù)據(jù),就像退出提示框一樣伏伐,只有宠进,取消和確定操作,有些dialog是需要數(shù)據(jù)的藐翎,這些數(shù)據(jù)決定dialog的具體的item項(xiàng))
3定義接口材蹬,添加監(jiān)聽,實(shí)現(xiàn)交互功能吝镣;
第一種簡(jiǎn)單提示框的實(shí)現(xiàn)(不需要從外界獲取數(shù)據(jù)):
效果演示:
<strong>第一步:定義dialog的布局文件layout_moresetdialog.xml堤器;</strong>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="@drawable/free_dialog_bg">
<TextView
android:id="@+id/set_more_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看更多"
android:textSize="14dp"
android:textColor="@color/blue"
android:gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
/>
<View
android:id="@+id/set_more_title_view"
android:layout_below="@+id/set_more_title"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#e6e6e6" />
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_below="@+id/set_more_title_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/hand_up_btn"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="舉手/放下"
android:background="@null"
android:drawableLeft="@mipmap/hand_up"
android:gravity="left|center_vertical"
android:drawablePadding="28dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#e6e6e6" />
<Button
android:id="@+id/net_switch_btn"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="優(yōu)選網(wǎng)絡(luò)"
android:background="@null"
android:drawableLeft="@mipmap/icon_more_netswitch"
android:gravity="left|center_vertical"
android:drawablePadding="30dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#e6e6e6" />
<Button
android:id="@+id/error_report_btn"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="故障報(bào)告"
android:background="@null"
android:drawableLeft="@mipmap/icon_more_about"
android:gravity="left|center_vertical"
android:drawablePadding="30dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#e6e6e6" />
<Button
android:id="@+id/play_switch_btn"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="切換"
android:background="@null"
android:drawableLeft="@mipmap/icon_more_switch"
android:gravity="left|center_vertical"
android:drawablePadding="30dp"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/dialog_close_btn"
android:layout_below="@+id/scrollview"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="30dp"
android:text="關(guān)閉"
android:textColor="@color/black"
android:background="@drawable/close_btn_bg"
/>
</LinearLayout>
</LinearLayout>
<strong>第二步:為每個(gè)item項(xiàng)定義監(jiān)聽,供外界調(diào)用末贾,提供相應(yīng)的事件響應(yīng)</strong>
/**
* 提供設(shè)置監(jiān)聽的方法
*/
public void setOnBtnClickListener(OnBtnClickListener listener){
this.listener=listener;
}
/**
* item項(xiàng)事件監(jiān)聽接口
*/
public interface OnBtnClickListener{
public void onHandUp();
public void onNetSwitch();
public void onErrorReport();
public void onPlaySwitch();
}
private void onClose(){
if (this.isShowing()){
dismiss();
}
}
完整代碼如下:
/**
* @author: Created by lzl on 2017/6/13.
* @function:
* @description:
*/
public class MoreSetDialog extends Dialog {
private Button mHandup;//舉手放下
private Button mNetSwitch;//優(yōu)選網(wǎng)絡(luò)
private Button mErrorReport;//錯(cuò)誤報(bào)告
private Button mPlaySwitch;//視頻與文檔切換
private Button mCloseBtn;//關(guān)閉
private OnBtnClickListener listener;//內(nèi)部監(jiān)聽器
public MoreSetDialog(Context context) {
super(context, R.style.MyDialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_moresetdialog);
//點(diǎn)擊外部區(qū)域不可取消
setCanceledOnTouchOutside(false);
initView();
initEvent();
}
private void initEvent() {
//舉手/放下事件
mHandup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener!=null){
listener.onHandUp();
}
}
});
//網(wǎng)絡(luò)優(yōu)選事件
mNetSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener!=null){
listener.onNetSwitch();
}
}
});
//錯(cuò)誤報(bào)告事件
mErrorReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener!=null){
listener.onErrorReport();
}
}
});
//視頻模塊與文檔模塊切換事件
mPlaySwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener!=null){
listener.onPlaySwitch();
}
}
});
//關(guān)閉事件
mCloseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onClose();
}
});
}
/**
* 初始化布局文件
*/
private void initView() {
mHandup= (Button) findViewById(R.id.hand_up_btn);
mNetSwitch= (Button) findViewById(R.id.net_switch_btn);
mErrorReport= (Button) findViewById(R.id.error_report_btn);
mPlaySwitch= (Button) findViewById(R.id.play_switch_btn);
mCloseBtn= (Button) findViewById(R.id.dialog_close_btn);
}
/**
* 提供設(shè)置監(jiān)聽的方法
*/
public void setOnBtnClickListener(OnBtnClickListener listener){
this.listener=listener;
}
/**
* item項(xiàng)事件監(jiān)聽接口
*/
public interface OnBtnClickListener{
public void onHandUp();
public void onNetSwitch();
public void onErrorReport();
public void onPlaySwitch();
}
private void onClose(){
if (this.isShowing()){
dismiss();
}
}
}
調(diào)用如下:
private void shoDialog() {
final MoreSetDialog dialog=new MoreSetDialog(this);
dialog.setOnBtnClickListener(new MoreSetDialog.OnBtnClickListener() {
@Override
public void onHandUp() {
Toast.makeText(getApplicationContext(),"舉手",Toast.LENGTH_SHORT).show();
}
@Override
public void onNetSwitch() {
dialog.dismiss();
Toast.makeText(getApplicationContext(),"優(yōu)選網(wǎng)絡(luò)",Toast.LENGTH_SHORT).show();
GsNetSwitchDialog gsNetSwitchDialog=new GsNetSwitchDialog(MoreDialogActivity.this);
gsNetSwitchDialog.show();
}
@Override
public void onErrorReport() {
Toast.makeText(getApplicationContext(),"故障報(bào)告",Toast.LENGTH_SHORT).show();
}
@Override
public void onPlaySwitch() {
Toast.makeText(getApplicationContext(),"切換",Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
引用的自定義drawable文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--背景色-->
<solid android:color="#ffffff" />
<!--描邊-->
<stroke
android:width="0.8dp"
android:color="#ffffff" />
<!-- 圓角 -->
<corners android:radius="10dp" />
</shape>
dialog的style屬性:
<!--自定義dialog背景全透明無(wú)邊框theme -->
<style name="MyDialog" parent="android:style/Theme.Dialog">
<!--背景顏色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除標(biāo)題 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除邊框-->
<item name="android:windowFrame">@null</item>
<!--是否浮現(xiàn)在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
一些注意的問題:
//點(diǎn)擊外部區(qū)域不可取消,false時(shí)表示點(diǎn)擊外部區(qū)域不隱藏該dialog闸溃,true時(shí)點(diǎn)擊外部區(qū)域取消該dialog
setCanceledOnTouchOutside(false);
2內(nèi)部的監(jiān)聽接口最好定義接口,具體響應(yīng)操作由外部調(diào)用者實(shí)現(xiàn)