国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)

2019-12-12 02:07:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文著重研究了自定義對(duì)話框,通過(guò)一下步驟即可清晰的理解原理,通過(guò)更改界面設(shè)置和style類(lèi)型,可以應(yīng)用在各種各樣適合自己的App中。

首先來(lái)看一下效果圖:

首先是activity的界面

點(diǎn)擊了上述圖片的按鈕后,彈出對(duì)話框:

點(diǎn)擊對(duì)話框的確定按鈕:

點(diǎn)擊對(duì)話框的取消按鈕:

下面來(lái)說(shuō)一下具體實(shí)現(xiàn)步驟:

第一步:設(shè)置Dialog的樣式(一般項(xiàng)目都可以直接拿來(lái)用):style.xml中

<!--自定義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>

第二步:自定義Dialog:

(1)自定義的Dialog的布局:dialog.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#11ffffff"><LinearLayout android:layout_width="260dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/free_dialog_bg" android:orientation="vertical"><TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="警告!!!" android:textColor="#38ADFF" android:textSize="16sp"/> <TextView  android:id="@+id/message"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_marginLeft="20dp"  android:layout_marginRight="20dp"  android:layout_gravity="center"  android:text="您的手機(jī)馬上自爆"/> <View  android:layout_width="match_parent"  android:layout_height="1px"  android:layout_marginTop="15dp"  android:background="#E4E4E4"/> <LinearLayout  android:layout_width="match_parent"  android:layout_height="40dp"  android:orientation="horizontal">  <Button   android:id="@+id/no"   android:layout_width="0dp"   android:layout_height="match_parent"   android:layout_weight="1"   android:layout_marginLeft="10dp"   android:background="@null"   android:gravity="center"   android:lines="1"   android:text="取消"   android:textColor="#7D7D7D"   android:textSize="16sp"/>  <View   android:layout_width="1px"   android:layout_height="match_parent"   android:background="#E4E4E4"/>  <Button   android:id="@+id/yes"   android:layout_width="0dp"   android:layout_height="match_parent"   android:layout_weight="1"   android:layout_marginRight="10dp"   android:background="@null"   android:gravity="center"   android:lines="1"   android:text="確定"   android:textColor="#38ADFF"   android:textSize="16sp"/> </LinearLayout></LinearLayout></RelativeLayout>

(2)自定義Dialog布局中的背景:free_dialog_bg.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <solid android:color="#ffffff" /> <stroke  android:width="0.8dp"  android:color="#ffffff" /> <!-- 圓角 --> <corners android:radius="6dp" /></shape>

(3) 自定義的Dialog的java:MyDialog.class

package com.syah.mydialog;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.support.annotation.NonNull;import android.support.annotation.StyleRes;import android.view.View;import android.widget.Button;import android.widget.TextView;/** * 創(chuàng)建自定義的Dialog,主要學(xué)習(xí)實(shí)現(xiàn)原理 * Created by admin on 2017/8/30. */public class MyDialog extends Dialog { private Button yes;//確定按鈕 private Button no;//取消按鈕 private TextView titleTV;//消息標(biāo)題文本 private TextView message;//消息提示文本 private String titleStr;//從外界設(shè)置的title文本 private String messageStr;//從外界設(shè)置的消息文本 //確定文本和取消文本的顯示的內(nèi)容 private String yesStr, noStr; private onNoOnclickListener noOnclickListener;//取消按鈕被點(diǎn)擊了的監(jiān)聽(tīng)器 private onYesOnclickListener yesOnclickListener;//確定按鈕被點(diǎn)擊了的監(jiān)聽(tīng)器 public MyDialog(@NonNull Context context, @StyleRes int themeResId) {  super(context, themeResId); } /**  * 設(shè)置取消按鈕的顯示內(nèi)容和監(jiān)聽(tīng)  *  * @param str  * @param onNoOnclickListener  */ public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {  if (str != null) {   noStr = str;  }  this.noOnclickListener = onNoOnclickListener; } /**  * 設(shè)置確定按鈕的顯示內(nèi)容和監(jiān)聽(tīng)  *  * @param str  * @param yesOnclickListener  */ public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {  if (str != null) {   yesStr = str;  }  this.yesOnclickListener = yesOnclickListener; } @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.dialog);  //空白處不能取消動(dòng)畫(huà)  setCanceledOnTouchOutside(false);  //初始化界面控件  initView();  //初始化界面數(shù)據(jù)  initData();  //初始化界面控件的事件  initEvent(); } /**  * 初始化界面控件  */ private void initView() {  yes = findViewById(R.id.yes);  no = findViewById(R.id.no);  titleTV = (TextView) findViewById(R.id.title);  message = (TextView) findViewById(R.id.message); } /**  * 初始化界面控件的顯示數(shù)據(jù)  */ private void initData() {  //如果用戶自定了title和message  if (titleStr != null) {   titleTV.setText(titleStr);  }  if (messageStr != null) {   message.setText(messageStr);  }  //如果設(shè)置按鈕文字  if (yesStr != null) {   yes.setText(yesStr);  }  if (noStr != null) {   no.setText(noStr);  } } /**  * 初始化界面的確定和取消監(jiān)聽(tīng)  */ private void initEvent() {  //設(shè)置確定按鈕被點(diǎn)擊后,向外界提供監(jiān)聽(tīng)  yes.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {    if (yesOnclickListener != null) {     yesOnclickListener.onYesOnclick();    }   }  });  //設(shè)置取消按鈕被點(diǎn)擊后,向外界提供監(jiān)聽(tīng)  no.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {    if (noOnclickListener != null) {     noOnclickListener.onNoClick();    }   }  }); } /**  * 從外界Activity為Dialog設(shè)置標(biāo)題  *  * @param title  */ public void setTitle(String title) {  titleStr = title; } /**  * 從外界Activity為Dialog設(shè)置message  *  * @param message  */ public void setMessage(String message) {  messageStr = message; } public interface onNoOnclickListener {  public void onNoClick(); } public interface onYesOnclickListener {  public void onYesOnclick(); }}

第三步:activity中使用自定義對(duì)話框:

(1)activity的布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.syah.mydialog.MainActivity"> <Button  android:id="@+id/btn"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="自定義dialog"  app:layout_constraintBottom_toBottomOf="parent"  app:layout_constraintLeft_toLeftOf="parent"  app:layout_constraintRight_toRightOf="parent"  app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>

(2) MainActivity.class

package com.syah.mydialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private MyDialog myDialog; private Button button; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  requestWindowFeature(Window.FEATURE_NO_TITLE);  setContentView(R.layout.activity_main);  button = (Button) findViewById(R.id.btn);  button.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {    myDialog=new MyDialog(MainActivity.this,R.style.MyDialog);    myDialog.setTitle("警告!");    myDialog.setMessage("警告:您的手機(jī)3秒鐘內(nèi)自爆");    myDialog.setYesOnclickListener("確定", new MyDialog.onYesOnclickListener() {     @Override     public void onYesOnclick() {      Toast.makeText(getApplicationContext(),"拜拜,我們來(lái)生見(jiàn)",Toast.LENGTH_LONG).show();      myDialog.dismiss();     }    });    myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {     @Override     public void onNoClick() {      Toast.makeText(getApplicationContext(),"明智的選擇",Toast.LENGTH_LONG).show();      myDialog.dismiss();     }    });    myDialog.show();   }  }); }}

原理:

1、通過(guò)構(gòu)造方法給dialog設(shè)置一個(gè)主題 R.style.MyDialog , 主要設(shè)置dialog的顯示屬性,一般都是 全透明無(wú)邊框

2、然后在dialog的onCreate()方法中,用setContentView( R.layout.SelfDialog) 為dialog設(shè)置XML文件,我們就可以在layout文件中創(chuàng)建自定義的Dialog風(fēng)格。這里我就自定義了xml文件格式,實(shí)現(xiàn)了自定義的外觀風(fēng)格,不受系統(tǒng)的主題影響。

3、然后通過(guò)設(shè)置要為外界設(shè)置一些public 公開(kāi)的方法,來(lái)向自定義的dialog傳遞值。這里的title 和 message,都是可以通過(guò)外界傳值進(jìn)來(lái),進(jìn)行設(shè)置的。如下面的public 方法就是供外界activity來(lái)設(shè)置title和message的:

/**  * 從外界Activity為Dialog設(shè)置標(biāo)題  *  * @param title  */ public void setTitle(String title) {  titleStr = title; } /**  * 從外界Activity為Dialog設(shè)置message  *  * @param message  */ public void setMessage(String message) {  messageStr = message; }

在activity通過(guò)實(shí)例化Dialog后就可以設(shè)置titile和message了。

myDialog=new MyDialog(MainActivity.this);myDialog.setTitle("警告!");myDialog.setMessage("警告:您的手機(jī)3秒鐘內(nèi)自爆");

另外在MyDialog.class中通過(guò)下面構(gòu)造器可以更靈活的選擇Dialog的類(lèi)型

public MyDialog(@NonNull Context context, @StyleRes int themeResId) {  super(context, themeResId);  }

activity中使用自定義的dialog:

myDialog=new MyDialog(MainActivity.this,R.style.MyDialog);

4、最后,自定義的dialog中包含了一些按鈕的時(shí)候,這個(gè)時(shí)候要想讓按鈕有點(diǎn)擊事件,并且把這個(gè)點(diǎn)擊事件能夠傳遞給activity,讓acitvity做一些事情,這里就需要設(shè)置監(jiān)聽(tīng)接口,讓button的點(diǎn)擊事件能夠讓外界activity知道。如下面的代碼。

/**  * 確定按鈕接口  */ public interface onNoOnclickListener {  public void onNoClick(); } /**  * 取消按鈕接口  */ public interface onYesOnclickListener {  public void onYesOnclick(); }
private onNoOnclickListener noOnclickListener;//取消按鈕被點(diǎn)擊了的監(jiān)聽(tīng)器 private onYesOnclickListener yesOnclickListener;//確定按鈕被點(diǎn)擊了的監(jiān)聽(tīng)器 /**  * 設(shè)置取消按鈕的顯示內(nèi)容和監(jiān)聽(tīng)  *  * @param str  * @param onNoOnclickListener  */ public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {  if (str != null) {   noStr = str;  }  this.noOnclickListener = onNoOnclickListener; } /**  * 設(shè)置確定按鈕的顯示內(nèi)容和監(jiān)聽(tīng)  *  * @param str  * @param yesOnclickListener  */ public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {  if (str != null) {   yesStr = str;  }  this.yesOnclickListener = yesOnclickListener; }
//設(shè)置確定按鈕被點(diǎn)擊后,向外界提供監(jiān)聽(tīng)  yes.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {    if (yesOnclickListener != null) {     yesOnclickListener.onYesOnclick();    }   }  });  //設(shè)置取消按鈕被點(diǎn)擊后,向外界提供監(jiān)聽(tīng)  no.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View v) {    if (noOnclickListener != null) {     noOnclickListener.onNoClick();    }   }  });

activity就可以設(shè)置監(jiān)聽(tīng)接口來(lái)實(shí)時(shí)獲取button的點(diǎn)擊事件如下:

myDialog.setYesOnclickListener("確定", new MyDialog.onYesOnclickListener() {     @Override     public void onYesOnclick() {      Toast.makeText(getApplicationContext(),"拜拜,我們來(lái)生見(jiàn)",Toast.LENGTH_LONG).show();      myDialog.dismiss();     }    });    myDialog.setNoOnclickListener("取消", new MyDialog.onNoOnclickListener() {     @Override     public void onNoClick() {      Toast.makeText(getApplicationContext(),"明智的選擇",Toast.LENGTH_LONG).show();      myDialog.dismiss();     }    });

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 天全县| 荥阳市| 阜新| 东乌珠穆沁旗| 鱼台县| 叶城县| 柳河县| 通许县| 左权县| 通州市| 唐海县| 开平市| 油尖旺区| 罗源县| 洞口县| 彭山县| 灵台县| 扎鲁特旗| 丹棱县| 广水市| 奉节县| 烟台市| 荣成市| 应城市| 安庆市| 高碑店市| 鹤庆县| 乐清市| 河北省| 开平市| 丘北县| 界首市| 开封县| 怀化市| 九龙县| 云安县| 孙吴县| 景德镇市| 磴口县| 通辽市| 铜川市|