对话框


一个非常简易的对话框

AlertDialog dialog=new AlertDialog.Builder(mcontext)
        .setTitle("我是对话框")//设置对话框的标题
        .setMessage("我是对话框的内容")//设置对话框的内容
        //设置对话框的按钮
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(mcontext, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        })
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(mcontext, "点击了确定的按钮", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        }).create();
dialog.show();

那么如何设置按钮的颜色呢:(要在show()后面调用)

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(theme);
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(theme);

还可以修改标题的颜色

try {
                    Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
                    mAlert.setAccessible(true);
                    Object mAlertController = mAlert.get(dialog);
                    //通过反射修改title字体大小和颜色
                    Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
                    mTitle.setAccessible(true);
                    TextView mTitleView = (TextView) mTitle.get(mAlertController);
                    mTitleView.setTextSize(32);
                    mTitleView.setTextColor(Color.RED);
                    //通过反射修改message字体大小和颜色
                    Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
                    mMessage.setAccessible(true);
                    TextView mMessageView = (TextView) mMessage.get(mAlertController);
                    mMessageView.setTextSize(28);
                    mMessageView.setTextColor(Color.GREEN);
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                } catch (NoSuchFieldException e2) {
                    e2.printStackTrace();

文章作者: 小游
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小游 !
  目录