android实现自定义DigLog

1.android内置的普通对话框比较枯燥 往往需要自定义DiaLog
需要的大致内容如下
(源码下载地址http://download.csdn.net/detail/gywuhengy/9816960
(1)想要设置的DiaLog布局

<?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="@color/possible_result_points"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提示"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tv_dialog_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:textSize="20dp"
        android:text=""
        android:layout_marginTop="40dp"
        android:layout_marginBottom="40dp"/>
    <LinearLayout
        android:layout_marginBottom="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        >
        <Button
            android:id="@+id/btn_diaLog_cancle"
            android:layout_marginRight="20dp"
            android:text="取消"
            android:textSize="20dp"
            android:background="@drawable/background_cancle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_diaLog_ok"
            android:text="确定"
            android:textSize="20dp"
            android:background="@drawable/background_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

(2)按钮的点击状态改变

<?xml version="1.0" encoding="utf-8"?>
<!--按钮点击不同状态可用图片和颜色-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--点击态--><item android:drawable="@color/colorAccent" android:state_pressed="true" />
    <!--正常态--><item android:drawable="@color/colorWhite" />
</selector>

(3)自定义工具类用于处理Dialog

public class Util_inflater {

    private static AlertDialog mAlertDialog;

    /**
     * @param layout
     * @return 工具类尽量将方法全都设置成静态的static
     */
    public static View getView(int layout, Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layout, null);
        return view;

    }

    /**
     * 显示自定义对话框
     *
     * @param context
     * @param message
     * @param diaLogListener
     */
    public static void showDiaLog(Context context, String message, final DiaLogListener diaLogListener) {

        /*View dialogView = null;
        dialogView = getView(R.layout.dialog_view, context);*/

        //打气筒
        View dialogView = View.inflate(context, R.layout.dialog_view, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        Button btn_diaLog_cancle = (Button) dialogView.findViewById(R.id.btn_diaLog_cancle);
        Button btn_diaLog_ok = (Button) dialogView.findViewById(R.id.btn_diaLog_ok);
        TextView tv_dialog_msg = (TextView) dialogView.findViewById(R.id.tv_dialog_msg);

        tv_dialog_msg.setText(message);
        //点击事件

        //取消
        btn_diaLog_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断对话框是否为空 关闭 点击空白
                if (mAlertDialog != null) {
                    mAlertDialog.cancel();
                }
            }
        });

        //确定
        btn_diaLog_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断对话框是否为空 点击完关闭
                if (mAlertDialog != null) {
                    mAlertDialog.cancel();
                }
                if (diaLogListener != null) {
                    diaLogListener.onClick();
                }
            }
        });
        //为dialog设置view
        builder.setView(dialogView);
        mAlertDialog = builder.create();
        //显示
        mAlertDialog.show();

    }
}

(4)调用一下

 Util_inflater.showDiaLog(this, "你确定要说出真心话?", new DiaLogListener() {
            @Override
            public void onClick() {
                Toast.makeText(DiaLogActivity.this, "我爱你!", Toast.LENGTH_SHORT).show();
            }
        });

(5)效果图如下

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/gywuhengy/article/details/70215126
今日推荐