2023-04-28 Android AlertDialog的使用 修改标题、内容、按钮的字体大小和字体颜色,自定义界面带输入框等,实现点击AlertDialog上按钮时不关闭对话框的方法.

一、效果一

     1.1界面如下

      1.2 代码

    private void NoPermissionsHintDialog() {
        AlertDialog.Builder alert_builder = new AlertDialog.Builder(m_context);
        alert_builder.setTitle("Warning")
                .setIcon(R.drawable.warning_icon)
                .setMessage("Please grant permissions firstly!")
                //.setView(inputServer)
                //.setView(mView)
                .setNegativeButton("Quit", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        exit(0);
                    }
                })
                .setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d(TAG,"m_JumpPermissionManagement");

                    }
                });

        alert_builder.setCancelable(false);
        //alert_builder.show();
        AlertDialog dialog = alert_builder.create();
        dialog.show();
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setAllCaps(false);
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setAllCaps(false);
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
    }

      1.3 代码解析,下面这一行是点击对话框外面的时候不退出。

 alert_builder.setCancelable(false);

      1.4 代码解析,下面这两行是不让两个按钮显示的字符都是大写,没有这两行,显示都是大写。

dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setAllCaps(false);
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setAllCaps(false);

      1.5 代码解析,下面这两行是让按钮改变颜色

dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));

二、效果二,带输入框

       2.1 效果图

        2.2 代码

                final EditText inputServer = new EditText(m_context);
                View mView = LayoutInflater.from(m_context).inflate(R.layout.enterpassword, null);
                final TextView m_password_string = (TextView) mView.findViewById(R.id.password_string);
                final EditText input_password = (EditText) mView.findViewById(R.id.password);
                m_password_string.setText("Enter the password for \"" + ssid + "\"");
                AlertDialog.Builder alert_builder = new AlertDialog.Builder(m_context);
                // builder.setTitle("Enter the password for \"" + ssid+ "\"")
                alert_builder.setTitle("Settings")
                        .setIcon(R.drawable.setting)
                        //.setView(inputServer)
                        .setView(mView)
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("Join", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (m_updatenetstatusthread.isAlive()) {
                                    m_updatenetstatusthread.interrupt();
                                }
                                String m_password = input_password.getText().toString();
                                m_wifi_list_loading_icon.setVisibility(View.VISIBLE);
                                handler.sendEmptyMessageDelayed(BLE_SET_WIFI_PASSWORD_TIMEOUT_MESSAGE, 20000);
                                sendCmdWifiSetup(ssid, m_password, "123");
                                builder = new StringBuilder();
                            }
                        });
                // builder.show();
                alert_builder.setCancelable(false);
                AlertDialog dialog = alert_builder.create();
                dialog.show();
                dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
                dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));

                try {
                    //获取mAlert对象
                    Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
                    mAlert.setAccessible(true);
                    Object mAlertController = mAlert.get(dialog);

                    //获取mTitleView并设置大小颜色
                    Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
                    mTitle.setAccessible(true);
                    TextView mTitleView = (TextView) mTitle.get(mAlertController);
                    //mTitleView.setTextSize(40);
                    mTitleView.setTextColor(getResources().getColor(R.color.colorPrimary));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }

         2.3  Android实现点击AlertDialog上按钮时不关闭对话框的方法,先检查输入的值是否有效,先setPositiveButton("Join", null);然后再getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener。

         2.4 enterpassword 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/password_string"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Password "
        android:textColor="@color/colorPrimary"
        android:textSize="15dp" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="82255222"
        android:textSize="15dp"
        android:textColor="#333333"
        android:padding="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:digits="@string/rule_pwd"
        android:hint="Password"
        android:background="@drawable/tv_bg"/>

</LinearLayout>

三、参考文章

Android实现点击AlertDialog上按钮时不关闭对话框的方法 - 建站教程

android 带EditView(编辑框)的AlertDialog(对话框)及获取输入内容_editview获取文字_非琴不是筝的博客-CSDN博客

Android AlertDialog修改标题、内容、按钮的字体大小和字体颜色_alertdialog 字体大小_yechaoa的博客-CSDN博客

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值