溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android自定義Dialog實(shí)現(xiàn)通用圓角對(duì)話(huà)框

發(fā)布時(shí)間:2020-09-16 18:54:48 來(lái)源:腳本之家 閱讀:230 作者:ruancw 欄目:移動(dòng)開(kāi)發(fā)

前言:圓角對(duì)話(huà)框在項(xiàng)目中用的越來(lái)越多,之前一篇文章有介紹過(guò)使用系統(tǒng)的AlertDialog+CardView(Android中使用CardView實(shí)現(xiàn)圓角對(duì)話(huà)框)實(shí)現(xiàn)了圓角對(duì)話(huà)框的樣式,今天介紹自定義Dialog實(shí)現(xiàn)通用的圓角對(duì)話(huà)框。

效果圖:

Android自定義Dialog實(shí)現(xiàn)通用圓角對(duì)話(huà)框

1.繼承自AlertDialog,重寫(xiě)onCreat

/**
 * Created by ruancw on 2018/6/7.
 * 自定義的帶圓角的對(duì)話(huà)框
 */

public class RoundCornerDialog extends AlertDialog{

  private TextView tvTitle;
  private TextView tvDes;
  private TextView tvCancel;
  private TextView tvConfirm;
  //private Context context;

  /**
   * 一個(gè)參數(shù)的構(gòu)造方法
   * @param context 上下文對(duì)象
   */
  public RoundCornerDialog(@NonNull Context context) {
    super(context);
    //this.context=context;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_layout_test);
    //設(shè)置背景透明,不然會(huì)出現(xiàn)白色直角問(wèn)題
    Window window = getWindow();
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    setCanceledOnTouchOutside(false);
    //初始化布局控件
    initView();
    //確定和取消按鈕的事件監(jiān)聽(tīng)
    initEvent();
    //設(shè)置參數(shù)必須在show之后,不然沒(méi)有效果
    WindowManager.LayoutParams params = getWindow().getAttributes();
    getWindow().setAttributes(params);
  }

}

注:解決白色直角的問(wèn)題

(1)文中沒(méi)有使用style設(shè)置背景透明,直接在代碼中用的window.setBackgroundDrawable設(shè)置的背景透明,不然會(huì)出現(xiàn)遺留的四個(gè)角有白色直角的問(wèn)題。

(2)當(dāng)然也可以在構(gòu)造方法中這樣設(shè)置:super(context,R.style.CustomDialog)。

2.初始化布局

(1)布局文件(CradView實(shí)現(xiàn)圓角布局)

<android.support.v7.widget.CardView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="@dimen/dp_30"
  app:cardCornerRadius="@dimen/dp_10">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bg_mainWhite"
    android:orientation="vertical">

    <TextView
      android:id="@+id/tv_title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:padding="@dimen/dp_10"
      android:text="溫馨提示"
      android:textColor="@color/bg_mainWhite"
      android:textSize="@dimen/sp_18" />

    <View
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="@color/bg_line" />

    <TextView
      android:id="@+id/tv_des"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="@dimen/dp_20"
      android:textSize="@dimen/sp_18" />

    <View
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="@color/bg_line" />

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="@dimen/dp_48"
      android:orientation="horizontal">

      <TextView
        android:id="@+id/tv_cancel"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="取消"
        android:textSize="@dimen/sp_16" />

      <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@color/bg_line" />

      <TextView
        android:id="@+id/tv_confirm"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="確定"
        android:textSize="@dimen/sp_16" />
    </LinearLayout>
  </LinearLayout>

</android.support.v7.widget.CardView>

(2)初始化布局文件及設(shè)置參數(shù)

/**
 * 初始化布局文件及設(shè)置參數(shù)
 */
private void initView() {
  //對(duì)話(huà)框標(biāo)題
  tvTitle=findViewById(R.id.tv_title);
  //對(duì)話(huà)框描述信息
  tvDes=findViewById(R.id.tv_des);
  //確定按鈕和取消
  tvConfirm=findViewById(R.id.tv_confirm);
  tvCancel=findViewById(R.id.tv_cancel);
  
}

(3)設(shè)置事件監(jiān)聽(tīng)

讓自定義的dialog實(shí)現(xiàn)OnClickListener接口,然后設(shè)置確定及取消按鈕的事件監(jiān)聽(tīng)

/**
 * 確定及取消點(diǎn)擊事件
 */
private void initEvent() {
  tvConfirm.setOnClickListener(this);//確定
  tvCancel.setOnClickListener(this);//取消@Override
public void onClick(View view) {
  switch (view.getId()){
    case R.id.tv_confirm:
      dismiss();
      break;
    case R.id.tv_cancel:
      dismiss();
      break;
  }
}

寫(xiě)到這里,圓角對(duì)話(huà)框就實(shí)現(xiàn)了,但如果另一個(gè)頁(yè)面要求不同背景色,按鈕的文本也不是“確定”和“取消”呢,我們是不是又的重寫(xiě)定義dialog和設(shè)置布局文件呢,顯然這樣很麻煩,貌似與我們的標(biāo)題寫(xiě)的通用的圓角對(duì)話(huà)框也不相符啊,這似乎不太好吧。接下來(lái),我們進(jìn)行一番改造,打造通用的圓角對(duì)話(huà)框。

3.打造通用圓角對(duì)話(huà)框

(1)initView中設(shè)置初始參數(shù)

private String title="溫馨提示",message,confirmText="確定",cancelText="取消";
//默認(rèn)的標(biāo)題欄背景色
private int titleColorBg=Color.parseColor("#FF8200");
//默認(rèn)的確定和取消按鈕背景色
private int confirmColorBg=Color.parseColor("#F8F8F8");
private int cancelColorBg=Color.parseColor("#F8F8F8");
/**
 * 初始化布局文件及設(shè)置參數(shù)
 */
private void initView() {
  //對(duì)話(huà)框標(biāo)題
  tvTitle=findViewById(R.id.tv_title);
  //對(duì)話(huà)框描述信息
  tvDes=findViewById(R.id.tv_des);
  //確定按鈕和取消
  tvConfirm=findViewById(R.id.tv_confirm);
  tvCancel=findViewById(R.id.tv_cancel);
 /********************通用設(shè)置*********************/
  //設(shè)置標(biāo)題、描述及確定按鈕的文本內(nèi)容
  tvTitle.setText(title);
  tvDes.setText(message);
  tvConfirm.setText(confirmText);
  //設(shè)置標(biāo)題欄及確定、取消按鈕背景色
  tvTitle.setBackgroundColor(titleColorBg);
  tvConfirm.setBackgroundColor(confirmColorBg);
  tvCancel.setBackgroundColor(cancelColorBg);
}

(2)定義設(shè)置屬性方法

/**
 * 設(shè)置標(biāo)題欄文本
 * @param title 標(biāo)題
 */
public void setTitle(String title){
  this.title=title;
}

/**
 * 設(shè)置描述信息
 * @param message 描述信息
 */
public void setMessage(String message){
  this.message=message;
}

/**
 * 設(shè)置確定按鈕上的文本
 * @param confirmText 文本
 */
public void setConfirmText(String confirmText){
  this.confirmText=confirmText;
}

/**
 * 設(shè)置取消按鈕上的文本
 * @param cancelText 文本
 */
public void setCancelText(String cancelText){
  this.cancelText=cancelText;
}

/**
 * 設(shè)置標(biāo)題欄的背景
 * @param titleColorBg 背景色int
 */
public void setTitleBg(int titleColorBg){
  this.titleColorBg=titleColorBg;
}

/**
 * 確定按鈕背景色
 * @param confirmColorBg int背景色
 */
public void setConfirmBg(int confirmColorBg){
  this.confirmColorBg=confirmColorBg;
}

/**
 * 取消按鈕背景色
 * @param cancelColorBg int背景色
 */
public void setCancelBg(int cancelColorBg){
  this.cancelColorBg=cancelColorBg;
}

(3)定義接口,實(shí)現(xiàn)確定按鈕的點(diǎn)擊回調(diào)

private ConfirmListener confirmListener;

/**
 * 設(shè)置確定按鈕的監(jiān)聽(tīng)
 * @param confirmListener
 */
public void setConfirmListener(ConfirmListener confirmListener){
  this.confirmListener=confirmListener;
}

/**
 * 確定按鈕點(diǎn)擊的監(jiān)聽(tīng)接口
 */
public interface ConfirmListener{
  void onConfirmClick();
}

點(diǎn)擊“確定”回調(diào)方法

case R.id.tv_confirm:
  /************通用設(shè)置***********/
  //點(diǎn)擊確定按鈕回調(diào)
  confirmListener.onConfirmClick();
  dismiss();
  break;

一般點(diǎn)擊“取消”按鈕不做任何操作,只是關(guān)閉當(dāng)前彈出的對(duì)話(huà)框,所以這里不做點(diǎn)擊后回調(diào),當(dāng)然,點(diǎn)擊“確定”后執(zhí)行相關(guān)操作后也要關(guān)閉當(dāng)前dialog。

4.使用

RoundCornerDialog roundCornerDialog=new RoundCornerDialog(mContext);
//設(shè)置標(biāo)題,描述,文本等參數(shù)
roundCornerDialog.setTitle("溫馨提示");
roundCornerDialog.setMessage("退出當(dāng)前登錄后將要重新登錄!");
roundCornerDialog.setConfirmText("確認(rèn)退出");
//確定按鈕的點(diǎn)擊回調(diào)方法
roundCornerDialog.setConfirmListener(new roundCornerDialog.ConfirmListener() {
  @Override
  public void onConfirmClick() {
    Intent intent = new Intent(mContext, LoginActivity.class);
    startActivity(intent);
    UIUtil.toast("退出成功,請(qǐng)重新登錄");
    getActivity().finish();
  }
});
//顯示對(duì)話(huà)框
roundCornerDialog.show();

總結(jié):本文通過(guò)自定義Dialog+CardView的方式實(shí)現(xiàn)了通用的圓角對(duì)話(huà)框效果,使用也相對(duì)簡(jiǎn)單,測(cè)試中發(fā)現(xiàn)在Android5.0以下設(shè)置標(biāo)題欄背景色時(shí),標(biāo)題欄不會(huì)跟隨CardView的圓角。

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI