Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android 自定義Dialog工具類,androiddialog

Android 自定義Dialog工具類,androiddialog

編輯:關於android開發

Android 自定義Dialog工具類,androiddialog


    由於項目的需要,系統的彈出框已經不能滿足我們的需求,我們需要各式各樣的彈出框,這時就需要我們去自定義彈出框了。

新建布局文件 dialog_layout.xml,將下面內容復制進去

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/shape_linearlayout"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:gravity="center_vertical"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txt_dialog_message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:text="TextView" />
        </LinearLayout>

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="1dp" >

            <Button
                android:id="@+id/btn_dialog_go"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:background="@drawable/linearlayout"
                android:text="確定"
                android:textColor="@color/blue" />

            <View
                android:id="@+id/dialog_bg"
                android:layout_width="1dp"
                android:layout_height="40dp"
                android:background="@color/line" />

            <Button
                android:id="@+id/btn_dialog_add"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:background="@drawable/linearlayout"
                android:text="取消"
                android:textColor="@color/blue" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

 

新建java類DialogUtil.java

public class DialogUtil {

    private static DialogUtil dialogUtil;
    private Dialog dialog;
    private TextView message;
    private Button firstBtn;
    private Button secondBtn;
    private onButtnClickListener buttnClickListener;

    public static DialogUtil getDialogInstance(Context context) {
        if (null == dialogUtil) {
            dialogUtil = new DialogUtil(context);
        }
        return dialogUtil;
    }

    private DialogUtil(Context context) {
        initDialog(context);
    }

    public void setMessage(String message) {
        if (null != message && !message.equals("")) {
            this.message.setText(message);
        }

    }

    private void initDialog(Context context) {
        dialog = new Dialog(context);

        dialog.setContentView(R.layout.dialog_layout);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        dialog.setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtils.getScreenWidth(context)
                - ScreenUtils.getScreenWidth(context) / 4;
        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        window.setWindowAnimations(R.style.displayStyle);
        message = (TextView) dialog.findViewById(R.id.txt_dialog_message);
        firstBtn = (Button) dialog.findViewById(R.id.btn_dialog_go);
        secondBtn = (Button) dialog.findViewById(R.id.btn_dialog_add);
        firstBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != buttnClickListener)
                    buttnClickListener.onFirstBtnClick(v);
            }
        });
        secondBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != buttnClickListener)
                    buttnClickListener.onSecondBtnClick(v);
            }
        });
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dissmissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
           
        }
    }

    public void setOnButtnClickListener(onButtnClickListener buttnClickListener) {
        this.buttnClickListener = buttnClickListener;
    }

    public interface onButtnClickListener {
        public void onFirstBtnClick(View v);

        public void onSecondBtnClick(View v);
    }

}

dialog工具類采用的是單列的模式,調用的流程是

1、先獲取對象

DialogUtil.getDialogInstance(context);

2、設置文本信息

setMessage(String message);

3、設置按鈕的單機事件

setOnButtnClickListener(onButtnClickListener buttnClickListener);

4、顯示dialog

showDialog();

至此自定義彈出框就完成了

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved