AlertDialog,自己写的,算是一个工具吧,拿出来以后直接

2017-12-20  本文已影响7人  Richair_Ann
因为不同厂商生产的Android手机AlertDialog样式也是千奇百怪,为了统一风格,闲暇时间自己写了一个dialog 先看效果
dialog.gif
代码如下:
CommentDialog类
 public class CommentDialog extends Dialog implements View.OnClickListener {
private ComDialogListener comDialogListener;
private String title = null;
private String textContext = null;
private String leftMenu = null;
private String rightMenu = null;

/**
 * 标题
 */
private TextView tv_dialog_title;

/**
 * 提示内容
 */
private TextView tv_dialog_content;

/**
 * 左侧按钮
 */
private Button btn_left_menu;


/**
 * 右侧按钮
 */
private Button btn_right_menu;

public CommentDialog(Context context, ComDialogListener comDialogListener) {
    super(context);
    this.comDialogListener = comDialogListener;

}

/**
 * 公共的弹框构造函数
 *
 * @param context
 * @param themeResId
 * @param title
 * @param textContext
 * @param leftMenu
 * @param rightMenu
 * @param comDialogListener
 */
public CommentDialog(Context context, int themeResId, String title, String textContext, String leftMenu, String rightMenu, ComDialogListener comDialogListener) {
    super(context, themeResId);
    this.title = title;
    this.textContext = textContext;
    this.leftMenu = leftMenu;
    this.rightMenu = rightMenu;
    this.comDialogListener = comDialogListener;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_comment);
    setCanceledOnTouchOutside(false);
  // hideBottomUIMenu();
    initViews();
}

/**
 * 处理全屏幕手机虚拟键
 */
private void hideBottomUIMenu() {
    //隐藏虚拟按键,并且全屏
    if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
        View v = this.getWindow().getDecorView();
        v.setSystemUiVisibility(View.GONE);
    } else if (Build.VERSION.SDK_INT >= 19) {
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }
}

/**
 * 初始化控件
 */
private void initViews() {
    tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title);
    tv_dialog_content = (TextView) findViewById(R.id.tv_dialog_content);
    btn_left_menu = (Button) findViewById(R.id.btn_cancel_menu);
    btn_right_menu = (Button) findViewById(R.id.btn_confirm_menu);
    if (null != title && !title.equals("")) {
        tv_dialog_title.setText(title);
    }

    if (leftMenu == null) {
        btn_left_menu.setVisibility(View.GONE);
    } else {
        btn_left_menu.setText(leftMenu);
        btn_left_menu.setOnClickListener(this);
    }
    tv_dialog_content.setText(textContext);


    if (rightMenu == null) {
        btn_right_menu.setVisibility(View.GONE);
    } else {
        btn_right_menu.setText(rightMenu);
        btn_right_menu.setOnClickListener(this);
    }
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btn_cancel_menu://左侧按钮
            if (null != comDialogListener) {
                comDialogListener.commentDialogCancelClick();
            }
            break;
        case R.id.btn_confirm_menu://右侧按钮
            if (null != comDialogListener) {
                comDialogListener.commentDialogConfirmClick();
            }
            break;
    }
}}
ComDialogListener 回调接口
public interface ComDialogListener {
/**
 * 点击左侧取消按钮事件处理
 */
void commentDialogCancelClick();


/**
 * 点击右侧确定按钮事件处理
 */
void commentDialogConfirmClick();
}
style样式
  <style name="dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.6</item>
</style>
dialog的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<LinearLayout
    android:layout_width="@dimen/margin_288"
    android:layout_height="@dimen/margin_192"
    android:layout_centerInParent="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_dialog_title"
        style="@style/text_style_18"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:paddingTop="@dimen/margin_10"
        android:text="提示"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/tv_dialog_content"
        style="@style/text_style_14"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="3"
        android:padding="@dimen/margin_10"
        android:textColor="@color/black" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="bottom"
        android:orientation="horizontal">


        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <Button
                android:id="@+id/btn_cancel_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <Button
                android:id="@+id/btn_confirm_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" />
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>
</RelativeLayout>
使用方式
CommentDialog  commentDialog = new CommentDialog(DilogActivity.this, R.style.dialog, "系统提示", "您的手机存在安全隐含,建议您晚上少玩手机,早点睡!", "取消", "确定", new ComDialogListener() {
                @Override
                public void commentDialogCancelClick() {
                    ToastUtil.showShort(DilogActivity.this,"你烦不烦");
                    commentDialog.dismiss();
                }

                @Override
                public void commentDialogConfirmClick() {
                    ToastUtil.showShort(DilogActivity.this,"好的知道了");
                    commentDialog.dismiss();
                }
            });
            commentDialog.show();

到此,弹框结束,如果觉得效果不好,可以让你们公司UI设计师帮你们设计好,这个只是掩饰,里边的布局等都可以修改!</font>

欢迎加入技术QQ群:46674429,366576264 
上一篇下一篇

猜你喜欢

热点阅读