1. 程式人生 > >安卓仿iOS確認框實現

安卓仿iOS確認框實現

效果圖

一、在layout下建立佈局 dialog_help.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:background="@drawable/bg_round_white"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/failure_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="@dimen/dp_12"
        android:layout_marginTop="@dimen/top"
        android:textSize="@dimen/text_size_b"
        android:textColor="@color/colorTextHint"/>

    <TextView
        android:id="@+id/failure_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:lineSpacingExtra="@dimen/dp_3"
        android:layout_marginLeft="@dimen/dp_20"

        android:layout_marginRight="@dimen/dp_20"
        android:layout_marginBottom="@dimen/dp_20"
        android:textSize="@dimen/text_size"
        android:textColor="@color/colorTextHint"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorBackGround"/>

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

        <TextView
            android:id="@+id/failure_back"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_dialog_left_white"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="返回"
            android:textSize="@dimen/text_size"
            android:textColor="@color/colorBlue"/>

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

        <TextView
            android:id="@+id/failure_ok"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_dialog_right_white"
            android:gravity="center"
            android:layout_weight="1.0"
            android:text="幫助"
            android:textSize="@dimen/text_size"
            android:textColor="@color/colorBlue"/>

    </LinearLayout>

</LinearLayout>

二、在drawable下建立

bg_round_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/colorWhite" />
    <corners android:radius="@dimen/dp_8" />
</shape>

bg_dialog_left_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/colorWhite" />
    <corners android:bottomLeftRadius="@dimen/dp_8" />
</shape>

bg_dialog_right_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/colorWhite" />
    <corners android:bottomRightRadius="@dimen/dp_8" />

</shape>

三、新建CommomDialog類 程式碼

package com.xinli.wenet.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.xinli.wenet.R;


/**
 * The type Commom dialog.
 */
public class CommomDialog extends Dialog implements View.OnClickListener{
    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;

    /**
     * Instantiates a new Commom dialog.
     *
     * @param context the context
     */
    public CommomDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    /**
     * Instantiates a new Commom dialog.
     *
     * @param context    the context
     * @param themeResId the theme res id
     * @param content    the content
     */
    public CommomDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    /**
     * Instantiates a new Commom dialog.
     *
     * @param context    the context
     * @param themeResId the theme res id
     * @param content    the content
     * @param listener   the listener
     */
    public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    /**
     * Instantiates a new Commom dialog.
     *
     * @param context        the context
     * @param cancelable     the cancelable
     * @param cancelListener the cancel listener
     */
    protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    /**
     * Set title commom dialog.
     *
     * @param title the title
     * @return the commom dialog
     */
    public CommomDialog setTitle(String title){
        this.title = title;
        return this;
    }

    /**
     * Set positive button commom dialog.
     *
     * @param name the name
     * @return the commom dialog
     */
    public CommomDialog setPositiveButton(String name){
        this.positiveName = name;
        return this;
    }

    /**
     * Set negative button commom dialog.
     *
     * @param name the name
     * @return the commom dialog
     */
    public CommomDialog setNegativeButton(String name){
        this.negativeName = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_help);
        setCanceledOnTouchOutside(false);
        Window window = getWindow();
        assert window != null;
        window.setGravity(Gravity.CENTER);  //此處可以設定dialog顯示的位置
       // window.setWindowAnimations(R.style.mystyle);  //新增動畫
        initView();
    }

    private void initView(){
        contentTxt = (TextView)findViewById(R.id.failure_content);
        titleTxt = (TextView)findViewById(R.id.failure_title);
        submitTxt = (TextView)findViewById(R.id.failure_ok);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView)findViewById(R.id.failure_back);
        cancelTxt.setOnClickListener(this);

        contentTxt.setText(content);
        if(!TextUtils.isEmpty(positiveName)){
            submitTxt.setText(positiveName);
        }

        if(!TextUtils.isEmpty(negativeName)){
            cancelTxt.setText(negativeName);
        }

        if(!TextUtils.isEmpty(title)){
            titleTxt.setText(title);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.failure_back:
                if(listener != null){
                    listener.onClick(this, false);
                }
                this.dismiss();
                break;
            case R.id.failure_ok:
                if(listener != null){
                    listener.onClick(this, true);
                }
                break;
        }
    }

    /**
     * The interface On close listener.
     */
    public interface OnCloseListener{
        /**
         * On click.
         *
         * @param dialog  the dialog
         * @param confirm the confirm
         */
        void onClick(Dialog dialog, boolean confirm);
    }
}

四、在新values下建樣式


    <style name="dialogs" 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>


        <!--模糊-->
    </style>

五、使用

 new CommomDialog(this, R.style.dialogs, "資料清除不可恢復,確定要清除資料嗎?", new CommomDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean confirm) {
                        if (confirm) {
                            dialog.dismiss();
             
                      }

                    }
                }).setTitle("清除資料提示").setPositiveButton("確定").show();

結束...