如何在程序代码中更改EditText边框线颜色?

问题描述 投票:3回答:2

其实我有2个问题。

  1. 如何更改edittext边框线的颜色和大小?其他答案如使用shape.xml来描述rect详细信息并将edit:text的背景设置为@ drawable / shape对我来说不起作用,因为我还需要更改焦点光标颜色。我尝试使用styles.xml并将colorAccent设置为线条颜色,并将EditText设置为android:theme。它适用于颜色,但我无法弄清楚如何在style.xml中更改行大小。
  2. 我想在运行时更改EditText的颜色,如何在程序代码中更改EditText线颜色和聚焦光标颜色?

enter image description here

任何帮助表示赞赏!非常感谢!

我想将白线大小更改为1 dp。

android android-edittext
2个回答
2
投票

您可以使用:

对于API级别21>21

ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat.getColor(this,R.color.red));
editText.setBackgroundTintList(colorStateList);

对于API级别< 21

editText.getBackground().mutate().setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.SRC_ATOP);

android:textCursorDrawable属性为@null将为您提供与您应用的EditText的文本颜色相同的光标颜色。

对于边界线:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
    android:width="1dp"
    android:color="#9ba3af"
    />
</shape>

0
投票
editText.getBackground().mutate().setColorFilter(getResources().getColor(R.color.AppColor), PorterDuff.Mode.SRC_ATOP);

同样android:textCursorDrawable属性@null应该导致使用android:textColor作为光标颜色。

在java类中使用此代码。

© www.soinside.com 2019 - 2024. All rights reserved.