手记

Android开发之写点击确认后的逻辑

效果图:

布局:

<?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:layout_gravity="center"
    android:background="@drawable/round_bg_fff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:padding="10dp"
        android:gravity="center"
        android:text="确定要删除吗?"
        android:textColor="#3a9be7"
        android:textSize="15sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#CFCFCF" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="取消"
            android:textColor="#666666"
            android:textSize="15sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#CFCFCF" />

        <TextView
            android:id="@+id/confirm"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="确定"
            android:textColor="#666666"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>

圆角xml(round_bg_fff.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 圆角半径 -->
    <corners android:radius="8dp" />

    <!-- 背景颜色 -->
    <solid android:color="#FFFFFF" />

</shape>

代码:

public class MyDialog {

      //点击确认按钮回调接口
    public interface OnConfirmListener {
        public void onConfirmClick();
    }

    /**
     * @Title: show
     * @Description: 显示Dialog
     * @param activity
     * @param content
     *            提示内容
     * @param confirmListener
     *            void
     * @throws
     */
    public static void show(Activity activity, String content,
            final OnConfirmListener confirmListener) {
        // 加载布局文件
        View view = View.inflate(activity, R.layout.dialog, null);
        TextView text = (TextView) view.findViewById(R.id.text);
        TextView confirm = (TextView) view.findViewById(R.id.confirm);
        TextView cancel = (TextView) view.findViewById(R.id.cancel);

        if (!StringUtil.isNullOrEmpty(content)) {
            text.setText(content);
        }

        // 创建Dialog
        final AlertDialog dialog = new AlertDialog.Builder(activity).create();
        dialog.setCancelable(false);// 设置点击dialog以外区域不取消Dialog
        dialog.show();
        dialog.setContentView(view);
        dialog.getWindow().setLayout(DensityUtil.getWidth(activity) / 3 * 2,
                LayoutParams.WRAP_CONTENT);//设置弹出框宽度为屏幕宽度的三分之二

        // 确定
        confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                confirmListener.onConfirmClick();
            }
        });
        // 取消
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

}

用的到的几个方法:

    /**
     * 判断字符串是否为null或空
     * 
     * @param string
     * @return true:为 空或null;false:不为 空或null
     */
    public static boolean isNullOrEmpty(String string) {
        boolean flag = false;
        if (null == string || string.trim().length() == 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * 获取屏幕宽度
     * 
     * @param activity
     * @return
     */
    public static int getWidth(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        return width;
    }

//使用

MyDialog.show(this, "确定退出登录吗?", new OnConfirmListener() {

                @Override
                public void onConfirmClick() {
                    //这里写点击确认后的逻辑
                }
            });
3人推荐
随时随地看视频
慕课网APP