android– 如何在EditText上以编程方式更改气泡(光标下)的颜色?

android– 如何在EditText上以编程方式更改气泡(光标下)的颜色?,第1张

概述我有EditText,并希望在代码上更改PROGRAMMATICALLY颜色.要更改光标的颜色,请使用thiscode.但是如何在代码上的EditViewPROGRAMMATICALLY上更改圆圈的颜色?解决方法:您将需要使用反射来对选择手柄(气泡)进行着色.我今天早上写了下面的课:用法示例:try{EditTextTint.applyCo

我有EditText,并希望在代码上更改PROGRAMMATICALLY颜色.

要更改光标的颜色,请使用this code.

但是如何在代码上的EditVIEw PROGRAMMATICALLY上更改圆圈的颜色?


解决方法:

您将需要使用反射来对选择手柄(气泡)进行着色.我今天早上写了下面的课:

用法示例:

try {  EditTextTint.applycolor(editText, color.CYAN);} catch (EditTextTint.EditTextTintError e) {  e.printstacktrace();}

EditTextTint.java:

import androID.content.res.Resources;import androID.graphics.PorterDuff;import androID.graphics.drawable.Drawable;import androID.support.annotation.colorInt;import androID.support.annotation.NonNull;import androID.Widget.EditText;import androID.Widget.TextVIEw;import java.lang.reflect.FIEld;/** * Tint the cursor and select handles of an {@link EditText} programmatically. */public class EditTextTint {  /**   * Set the cursor and handle colors for an {@link EditText} programmatically.   *   * @param editText   *     The {@link EditText} to tint   * @param color   *     The color to apply for the cursor and select handles   * @throws EditTextTintError   *     If an error occured while attempting to tint the vIEw.   */  public static voID applycolor(@NonNull EditText editText, @colorInt int color) throws EditTextTintError {    EditTextTint editTextTint = new Builder(editText)        .setCursorcolor(color)        .setSelectHandleleftcolor(color)        .setSelectHandleRightcolor(color)        .setSelectHandleMIDdlecolor(color)        .build();    editTextTint.apply();  }  private final EditText editText;  private final Integer cursorcolor;  private final Integer selectHandleleftcolor;  private final Integer selectHandleRightcolor;  private final Integer selectHandleMIDdlecolor;  private EditTextTint(Builder builder) {    editText = builder.editText;    cursorcolor = builder.cursorcolor;    selectHandleleftcolor = builder.selectHandleleftcolor;    selectHandleRightcolor = builder.selectHandleRightcolor;    selectHandleMIDdlecolor = builder.selectHandleMIDdlecolor;  }  /**   * Sets the color for the cursor and handles on the {@link EditText editText}.   *   * @throws EditTextTintError   *     if an error occurs while tinting the vIEw.   */  public voID apply() throws EditTextTintError {    try {      Resources res = editText.getContext().getResources();      // Get the editor      FIEld fIEld = TextVIEw.class.getDeclaredFIEld("mEditor");      fIEld.setAccessible(true);      Object editor = fIEld.get(editText);      if (cursorcolor != null) {        // Get the cursor drawable, tint it, and set it on the TextVIEw Editor        fIEld = TextVIEw.class.getDeclaredFIEld("mCursorDrawableRes");        fIEld.setAccessible(true);        int cursorDrawableRes = fIEld.getInt(editText);        Drawable cursorDrawable = res.getDrawable(cursorDrawableRes).mutate();        cursorDrawable.setcolorFilter(cursorcolor, PorterDuff.Mode.SRC_IN);        Drawable[] drawables = {cursorDrawable, cursorDrawable};        fIEld = editor.getClass().getDeclaredFIEld("mCursorDrawable");        fIEld.setAccessible(true);        fIEld.set(editor, drawables);      }      String[] resFIEldnames = {"mTextSelectHandleleftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes"};      String[] drawableFIEldnames = {"mSelectHandleleft", "mSelectHandleRight", "mSelectHandleCenter"};      Integer[] colors = {selectHandleleftcolor, selectHandleRightcolor, selectHandleMIDdlecolor};      for (int i = 0; i < resFIEldnames.length; i++) {        Integer color = colors[i];        if (color == null) {          continue;        }        String resFIEldname = resFIEldnames[i];        String drawableFIEldname = drawableFIEldnames[i];        fIEld = TextVIEw.class.getDeclaredFIEld(resFIEldname);        fIEld.setAccessible(true);        int selectHandleRes = fIEld.getInt(editText);        Drawable selectHandleDrawable = res.getDrawable(selectHandleRes).mutate();        selectHandleDrawable.setcolorFilter(color, PorterDuff.Mode.SRC_IN);        fIEld = editor.getClass().getDeclaredFIEld(drawableFIEldname);        fIEld.setAccessible(true);        fIEld.set(editor, selectHandleDrawable);      }    } catch (Exception e) {      throw new EditTextTintError("Error applying tint to " + editText, e);    }  }  public static class Builder {    final EditText editText;    Integer cursorcolor;    Integer selectHandleleftcolor;    Integer selectHandleRightcolor;    Integer selectHandleMIDdlecolor;    public Builder(@NonNull EditText editText) {      this.editText = editText;    }    public Builder setCursorcolor(@colorInt int cursorcolor) {      this.cursorcolor = cursorcolor;      return this;    }    public Builder setSelectHandleleftcolor(@colorInt int selectHandleleftcolor) {      this.selectHandleleftcolor = selectHandleleftcolor;      return this;    }    public Builder setSelectHandleRightcolor(@colorInt int selectHandleRightcolor) {      this.selectHandleRightcolor = selectHandleRightcolor;      return this;    }    public Builder setSelectHandleMIDdlecolor(@colorInt int selectHandleMIDdlecolor) {      this.selectHandleMIDdlecolor = selectHandleMIDdlecolor;      return this;    }    public EditTextTint build() {      return new EditTextTint(this);    }  }  public static class EditTextTintError extends Exception {    public EditTextTintError(String message, Throwable cause) {      super(message, cause);    }  }}

注意:这应该从Jelly Bean到Nougat.但是,由于它使用反射来获取和设置私有字段,因此在未来的AndroID版本中可能会中断,或者如果制造商对EditText进行了更改.

总结

以上是内存溢出为你收集整理的android – 如何在EditText上以编程方式更改气泡(光标下)的颜色?全部内容,希望文章能够帮你解决android – 如何在EditText上以编程方式更改气泡(光标下)的颜色?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1098082.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存