当前位置: 首页 > 知识库问答 >
问题:

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

燕承安
2023-03-14

我有EditText,希望通过编程更改代码的颜色。

为了改变光标的颜色,我使用这个代码。

共有3个答案

张啸
2023-03-14

试试这个:更改值/colors.xml文件中的值

<color name="colorAccent">#263238</color>

将此颜色代码#263238更改为您自己的颜色代码,以便它适用于所有项目。希望这对你有帮助。

彭展
2023-03-14

下面的方法适用于所有游标气泡,如左、右和中心。我的意思是,除了你的要求,它适用于左和右。

您可以通过删除两个数组中的左和右字段名,将该方法更改为只对中心句柄着色。

public static void colorHandles(TextView view, int color) {
  try {
    Field editorField = TextView.class.getDeclaredField("mEditor");
    if (!editorField.isAccessible()) {
      editorField.setAccessible(true);
    }

    Object editor = editorField.get(view);
    Class<?> editorClass = editor.getClass();

    String[] handleNames = {"mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter"};
    String[] resNames = {"mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes"};

    for (int i = 0; i < handleNames.length; i++) {
      Field handleField = editorClass.getDeclaredField(handleNames[i]);
      if (!handleField.isAccessible()) {
        handleField.setAccessible(true);
      }

      Drawable handleDrawable = (Drawable) handleField.get(editor);

      if (handleDrawable == null) {
        Field resField = TextView.class.getDeclaredField(resNames[i]);
        if (!resField.isAccessible()) {
          resField.setAccessible(true);
        }
        int resId = resField.getInt(view);
        handleDrawable = view.getResources().getDrawable(resId);
      }

      if (handleDrawable != null) {
        Drawable drawable = handleDrawable.mutate();
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        handleField.set(editor, drawable);
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}
甄志
2023-03-14

您将需要使用反射来着色选择句柄(气泡)。今天早上我写了以下类:

示例用法:

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);
    }
  }

}

注意:从果冻豆到牛轧糖都可以。然而,由于它使用反射来获取和设置私有字段,这可能会在未来的Android版本中中断,或者如果制造商对EditText进行了更改。

 类似资料:
  • 问题内容: 在android中,我们可以通过以下方式更改光标颜色: 。 我们如何动态地做到这一点? 在我的情况下,我将可绘制的光标设置为白色,但是我需要将其更改为黑色。怎么办? 问题答案: 使用一些反射对我有用 Java: XML: 您可以使用不需要XML的方法:

  • 在Android系统中,我们可以通过以下方式更改光标颜色: android:textCursorDrawable=“@drawable/black\u color\u cursor”。 我们如何动态地做到这一点? 在我的情况下,我已经将光标绘图为白色,但我需要改变黑色怎么办?

  • 如何根据android edittext的背景颜色(动态)更改android edittext光标颜色?。如果我选择深色背景颜色编辑文本,光标将是浅色。如果我选择深色背景颜色编辑文本,光标将是暗色。基于edittext颜色更改,光标颜色将在运行时更改。如何实现?任何想法都欢迎。

  • 然而,当我运行该应用程序时,我没有看到任何变化。更改背景本身: 将整个更改为-不是我想要的! 如何以编程方式更改、或的下划线颜色?我使用的是支持库的25.0.1版本。

  • 问题内容: 我想在字体为时使用光标。 那可能吗? 问题答案: 您可以定制一个。

  • 问题内容: 我不知道这有可能吗? 但是我想更改光标闪烁的颜色…通常是黑色的.... 我正在做一个基于java-swing的项目,在那…要求之一就是更改光标闪烁的颜色。 … 这可能吗? 问题答案: “光标”用于指不闪烁的鼠标光标。 因此,我假设您正在谈论文本组件中使用的插入符号: 编辑: 以上建议针对一个文本字段执行此操作。要为所有文本字段更改它,您应该能够在程序开始时使用以下命令: