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

时间:2016-11-30 11:55:44

标签: android android-edittext

其实我有2个问题。

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

  2. 我想在运行时更改EditText的颜色,如何更改EditText线颜色并将光标颜色聚焦在程序代码中?

  3. enter image description here

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

    我想将白线尺寸更改为1 dp。

2 个答案:

答案 0 :(得分:2)

您可以使用:

适用于API级>21ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat.getColor(this,R.color.red)); editText.setBackgroundTintList(colorStateList);

< 21

适用于API级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> 文字颜色相同的光标颜色。

对于边界线:

MFMailComposeViewController.canSendMail() // returns false for simulators.

答案 1 :(得分:0)

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

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

在java类中使用此代码。