【问题标题】:How To Change The Bottom Line EditText Color?如何更改底线 EditText 颜色?
【发布时间】:2019-10-14 00:18:30
【问题描述】:

我在AlertDialog 中有一个EditText,而底线颜色不是我想要的,我不知道如何更改它。

这是我目前所拥有的。

AlertDialog.Builder adb = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.EditTextAlertDialog));
    adb.setTitle("Title");
    inputET = new EditText(this);
    inputET.setInputType(InputType.TYPE_CLASS_TEXT);
    inputET.setTextColor(getResources().getColor(android.R.color.white));
   inputET.setHighlightColor(getResources().getColor(android.R.color.white)); //This should change it, but it's not.
    adb.setView(inputET);

    adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    adb.show();

由于某些奇怪的原因不起作用,应该是setHightlightColor

【问题讨论】:

  • 试试这个:editText.getBackground().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP);
  • 谢谢迪内什!现在我只需要弄清楚如何让占位符颜色发生变化。
  • 我的意思是光标颜色

标签: java android material-design android-alertdialog


【解决方案1】:

您可以为对话框创建样式。

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/yourEditTextColor</item>
    </style>

然后在创建对话框时添加它

AlertDialog.Builder adb = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.MyDialogTheme));

【讨论】:

  • 我已经尝试过了,但它不起作用。在代码中更改它 sn-p 似乎是它会改变的唯一方法。 Dinesh 的解决方案对我有用。
  • @lllcoderlll 好的。我曾将它用于具有 xml 布局的对话框(不是 AlertDialog),并且效果很好。猜猜它不适用于 AlertDialog。
【解决方案2】:

您需要为该用途设置背景色调:

inputET.setBackgroundTintList(ColorStateList.valueOf(intColorCode));

【讨论】:

    【解决方案3】:

    在xml文件下面这样放到你的EditText中:app:backgroundTint="@color/black"

    【讨论】:

      【解决方案4】:

      你可以做一些不同的事情。

      使用Filled Box 主题(默认)定义带有TextInputLayout 的布局,应用app:boxStrokeColor 属性来更改下划线的颜色。

      <FrameLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:paddingTop="?attr/dialogPreferredPadding"
          android:paddingStart="?attr/dialogPreferredPadding"
          android:paddingEnd="?attr/dialogPreferredPadding">
      
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            app:boxStrokeColor="@color/my_favorite_color"
            android:hint="Hint text">
      
          <com.google.android.material.textfield.TextInputEditText
              android:id="@android:id/text1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
        </com.google.android.material.textfield.TextInputLayout>
      </FrameLayout>
      

      然后使用定义的布局构建AlertDialog

      new MaterialAlertDialogBuilder(AlertDialogActivity.this)
                  .setTitle("Dialog")
                  .setMessage("Lorem ipsum dolor ....")
                  .setView(R.layout.custom_text_alert_dialog)
                  .setPositiveButton("Ok", /* listener = */ null)
                  .setNegativeButton("Cancel", /* listener = */ null)
                  .show();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-21
        • 1970-01-01
        • 1970-01-01
        • 2021-11-09
        • 2017-07-23
        相关资源
        最近更新 更多