《第二行代碼》 第一章AlertDialog

先看下效果圖
系統彈框:

自定義彈框:

下面分別對這兩種彈框進行說明:
系統彈框代碼,也沒什麼好說的,正式項目中基本用不到,因爲太醜了。

/**
 * @desc : 系統彈框
 * @author : congge on 2021-09-09 10:50
 **/
public void systemDialogClick(View view){
    new AlertDialog.Builder(mContext)
            .setTitle("提示")
            .setMessage("確定要刪除嘛?")
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(mContext,"你點擊了取消",Toast.LENGTH_LONG).show();
                }
            })
            .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(mContext,"你點擊了確定",Toast.LENGTH_LONG).show();
                }
            })
            .create()
            .show();
}  

我們重點來看下自定義彈框
步驟一:定義彈框的style

<style name="mydialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item><!--邊框-->
    <item name="android:windowIsFloating">true</item><!--是否浮現在activity之上-->
    <item name="android:windowIsTranslucent">false</item><!--是否透明-->
    <item name="android:windowNoTitle">true</item><!--無標題-->
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item><!--模糊-->
    <item name="android:backgroundDimAmount">0.6</item>
</style>  

看不懂沒關係,你就當固定的寫法
步驟二:設置彈框的View,通過設置dialog的window的setContentView
當然要先建layout

dialog_ok_cancle.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lLayout_bg"
android:layout_width="280dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:background="@drawable/dialog_window_bg"
android:orientation="vertical">

<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:gravity="center"
    android:textColor="#333333"
    android:text="提示"
    android:textSize="18sp" />
<View
    android:layout_width="match_parent"
    android:layout_height="1px"
    android:background="#ededed"
    android:layout_marginTop="10dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_message"
    android:textSize="14sp"
    android:textColor="#555555"
    android:gravity="center"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="32dp"
    android:layout_marginBottom="32dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:lineSpacingExtra="5dp"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:background="@drawable/dialog_window_cancel_bg"
        android:gravity="center"
        android:text="@string/cancel"
        android:textColor="@color/c_main"
        android:textSize="16sp" />


    <Button
        android:id="@+id/btn_ok"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:background="@drawable/dialog_window_ok_bg"
        android:gravity="center"
        android:text="@string/ok"
        android:textColor="#ffffff"
        android:textSize="16sp" />
</LinearLayout>


</LinearLayout>

調用代碼:

/**
 * @desc : 自定義彈框
 * @author : congge on 2021-09-09 11:01
 **/
public void customDialogClick(View view){
    okAndCancelDialog(mContext, "提示", "確定刪除嘛?", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext,"你點擊了確定",Toast.LENGTH_LONG).show();
        }
    });
}

private   void okAndCancelDialog(Context context, String title, String message,
                                 final View.OnClickListener okListener) {
    final android.app.AlertDialog dialog = new android.app.AlertDialog.Builder(context, R.style.mydialog).setCancelable(true).create();
    dialog.show();

    Window window = dialog.getWindow();
    window.setContentView(R.layout.dialog_ok_cancel);
    ((TextView) window.findViewById(R.id.tv_title)).setText(title);
    ((TextView) window.findViewById(R.id.tv_message)).setText(message);
    window.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();

        }
    });
    window.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();

            okListener.onClick(v);
        }
    });

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章