中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

自定義Dialog

發布時間:2020-06-02 23:21:10 來源:網絡 閱讀:482 作者:Sunny貍貍 欄目:移動開發

需要實現的效果圖:

    自定義Dialog 


自定義Dialog


一個項目所用到的對話框風格必須統一,但顯示的文字、布局卻可以不同。如果每個對話框都要重新去創建的話,會增加代碼的冗余,浪費資源。所以可以寫個類 MyDialog 繼承Dailog。需要的時候直接調用MyDialog類。


一、新建xml布局。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:textSize="18dp"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:textColor="#000000"
            android:textStyle="bold"
            android:text="InPut" />
        

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"  >

            <TextView
                android:id="@+id/tv_message"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:gravity="left|center"
                android:textSize="16dp"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:textColor="#333333"
                />
            <RelativeLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="left|center"
                >
                
            
             <EditText
                android:id="@+id/et_psw_message"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:gravity="left|center"
                android:inputType="textPassword"
                android:textColorHint="@color/gray"
                android:visibility="gone"
                android:maxLength="30" 
                android:textSize="@dimen/font_14"
                android:background="@drawable/edit_back">
                <requestFocus />
                </EditText>

             <ImageView
                 android:id="@+id/img_change_name"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentRight="true"
                 android:layout_marginRight="15dp"
                 android:layout_centerVertical="true"
                 android:visibility="gone"
                 android:src="@drawable/img_change_name_style" />

            </RelativeLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1.0px"
            android:background="#ffd0d0d0" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_gravity="bottom"
            android:background="#ffd0d0d0"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/negativeButton"
                android:textSize="17dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:background="#ffffff"
                android:layout_weight="1"
                android:text="Cancel" />

            <Button
                android:id="@+id/positiveButton"
                android:textSize="17dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="1px"
                android:background="#ffffff"
                android:gravity="center"
                android:layout_weight="1"
                android:text="Sure"
                android:textStyle="bold"
                android:textColor="#008ae7" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

xml效果圖如下:

    自定義Dialog

盡量將對話框要用到的所有控件在xml中排好,部分不需要的可以在代碼setVisibility(View.GONE);讓其消失,省的新建多個xml文件。


二、繼承Dailog

package com.dnake.evertalk.widgets;

import com.dnake.evertalk.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MyDialog extends Dialog {

    public MyDialog(Context context) {
        super(context);
    }

    public MyDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

      
        public Builder setMessage(int message) {//設置信息
            this.message = (String) context.getText(message);
            return this;
        }

    
        public Builder setTitle(int title) { //設置標題
            this.title = (String) context.getText(title);
            return this;
        }


        public Builder setTitle(String title) {//直接傳字符串
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         */
        public Builder setPositiveButton(int positiveButtonText,DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public void create(View layout) {
            final MyDialog dialog = new MyDialog(context,R.style.dialog_theme);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
            }
            
            if(message != null) {
                TextView hint = (TextView) layout.findViewById(R.id.tv_message);
                hint.setText(message);
            }
            
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
            }
            
            dialog.setContentView(layout);
            dialog.show();
        }

    }
}



三、在Activity中創建dialog,設置標題、信息以及按鈕的處理事件

    private void showDialog() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View layout = inflater.inflate(R.layout.dialog, null);
        
        Builder builder = new MyDialog.Builder(this);
        
        builder.setTitle(getResources().getString(R.string.mymessageactivity_delete_title));
        builder.setMessage(getResources().getString(R.string.mymessageactivity_delete_all_hint));
        
        builder.setPositiveButton(getResources().getString(R.string.sure),new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); 
                intData();
            }
        });

        builder.setNegativeButton(getResources().getString(R.string.cancel),
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builder.create(layout);
    }


這樣就實現了項目需要的對話框效果圖,且不同的Activity的對話框可顯示不同的文字,也可以處理不同的點擊事件。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

左云县| 恩平市| 石阡县| 通化县| 辽中县| 北碚区| 灵台县| 井研县| 龙海市| 安图县| 高清| 平安县| 蒲江县| 汶上县| 当阳市| 鹰潭市| 油尖旺区| 安龙县| 临西县| 丰县| 兰坪| 开原市| 界首市| 大关县| 宜兰县| 松江区| 会东县| 灵武市| 新巴尔虎左旗| 肇东市| 南安市| 鄱阳县| 德兴市| 芦溪县| 承德市| 区。| 涿州市| 惠水县| 嘉禾县| 珠海市| 和林格尔县|