以编程方式设置TextInputLayout样式

时间:2016-10-28 15:02:52

标签: android android-theme android-styles android-textinputlayout

有没有办法以编程方式自定义这些TextInputLayout属性:

  • textColorHint
  • colorAccent
  • colorControlNormal
  • colorControlActivated
  • textSelectHandle

我知道如何使用主题属性设置样式,但我正在处理的项目动态加载颜色信息,据我所知,无法在运行时更改主题/样式值。

1 个答案:

答案 0 :(得分:3)

我编写了textInputLayout实现,可以设置下划线和错误的任何颜色,并在顶部显示错误。

enter image description here

enter image description here

enter image description here

import org.apache.log4j.{Level, Logger}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext, TaskContext}

object Example {

  def main(args:Array[String]): Unit = {
    Logger.getLogger("org").setLevel(Level.WARN)
    Logger.getLogger("akka").setLevel(Level.WARN)

    val conf = new SparkConf()
      .setAppName("shuffle example")
      .setMaster("local[*]")
      .set("spark.task.maxFailures", "4") // it is default value

    val sc = new SparkContext(conf)


    val l:RDD[String] = sc.parallelize(List( "a", "b", "c"), 3)

    def myMap(v: String) = {
      // print task info and return "Ok" or throw exception
      val context = TaskContext.get()
      val r = scala.util.Random
      val raise = r.nextBoolean()
      println(s"--- map $v in partition ${context.partitionId()} in stage ${context.stageId()} raise = $raise")
      if ( raise )
        throw new Exception("oh ;(")
      "Ok"
    }

    println (l.map(myMap).collect().mkString("\n")) // failed 

    sc.stop()
  }
}

public class TextInputLayoutUseful extends TextInputLayout {
private CharSequence originalHint = "";

public TextInputLayoutUseful(Context context) {
    super(context);
    init();
}

public TextInputLayoutUseful(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public TextInputLayoutUseful(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    this.originalHint = getHint();
    setErrorEnabled(false);
}

/**
 * - put error text into top floating label
 * - colorized layout
 *
 * @param error error text
 */
@Override
public void setError(@Nullable CharSequence error) {
    if (error == null) {
        setHint(originalHint);
        setHintTextAppearance(R.style.InputLayoutNormalHint);
        setUnderlineColor(R.color.input_normal_accent);
    } else {
        setHint(error);
        setHintTextAppearance(R.style.InputLayoutErrorHint);
        setUnderlineColor(R.color.input_error_accent);
    }
}

/**
 * colorized layout specified green color
 *
 * @param acceptedHint text for floating label
 */
public void setGreenError(@NonNull CharSequence acceptedHint) {
    setHint(acceptedHint);
    setHintTextAppearance(R.style.InputLayoutAcceptedHint);
    setUnderlineColor(R.color.input_accepted_accent);
}

private void setUnderlineColor(@ColorRes int colorRes) {
    if (getEditText() != null) {
        getEditText().getBackground().mutate().setColorFilter(ContextCompat.getColor(getContext(), colorRes), PorterDuff.Mode.SRC_ATOP);
    }
}
}