无法自定义EditText的外观选择横向上的句柄/锚点

时间:2017-06-02 22:45:29

标签: android android-edittext

我很难自定义EditText选择句柄。我正在关注这个帖子:

How to change color / appearance of EditText select handle / anchor?

看起来很简单。然而,我无法让它在景观方面发挥作用。谁能发现我做错了什么?我几乎在测试活动上粘贴了相同的代码,但锚句柄总是相同的。我尝试使用建议和编程方式的样式。我总是得到相同的默认蓝色锚点:(

我在Nougat上不确定是否有任何区别。

测试活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.MyCustomTheme);
    setContentView(R.layout.activity_main);
    final EditText editText = (EditText) findViewById(R.id.edit1);
    // tried programatically too and no success
    try {
        final Field fEditor = TextView.class.getDeclaredField("mEditor");
        fEditor.setAccessible(true);
        final Object editor = fEditor.get(editText);
        final Field fSelectHandleLeft = editor.getClass().getDeclaredField("mSelectHandleLeft");
        final Field fSelectHandleRight =
                editor.getClass().getDeclaredField("mSelectHandleRight");
        final Field fSelectHandleCenter =
                editor.getClass().getDeclaredField("mSelectHandleCenter");
        fSelectHandleLeft.setAccessible(true);
        fSelectHandleRight.setAccessible(true);
        fSelectHandleCenter.setAccessible(true);
        fSelectHandleLeft.set(editor, ContextCompat.getDrawable(this, R.drawable.small_rect));
        fSelectHandleRight.set(editor, ContextCompat.getDrawable(this, R.drawable.small_rect));
        fSelectHandleCenter.set(editor, ContextCompat.getDrawable(this, R.drawable.small_rect));
    } catch (final Exception e) {
        Log.d("CUSTOM_ANCHORS", e.toString());
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Hello World"
        android:textSize="20sp" />

</LinearLayout>

我的风格:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>


<style name="MyCustomTheme" parent="@style/AppTheme">
    <item name="android:textSelectHandle">@drawable/small_rect</item>
    <item name="android:textSelectHandleLeft">@drawable/small_rect</item>
    <item name="android:textSelectHandleRight">@drawable/small_rect</item>
</style>

drawable(small_rect.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="20dp"
        android:height="20dp" />
    <gradient
        android:angle="90"
        android:centerColor="#D6D6D6"
        android:endColor="#4B6CD6"
        android:startColor="#6586F0" />
    <corners android:radius="0dp" />
</shape>

结果:

enter image description here

6 个答案:

答案 0 :(得分:3)

 <EditText
    android:imeOptions="flagNoFullscreen"
 />

您可以使用android:imeOptions告诉系统不要在全屏显示编辑用户界面。 enter image description here

答案 1 :(得分:2)

我认为您的自定义EditText句柄只能用xml实现。

  1. 在styles.xml中定义句柄的样式:

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Hello World"
        style="@style/CustomSelectHandle"
        android:textSize="20sp" />
    
  2. 将style属性添加到EditText:

    function createConverter() {
        var charRange = (start, end) => Array.from(Array(end-start), 
                                             (v, i) => String.fromCharCode(i + start))
    
        var digits = charRange(48, 48+10)             // 0 to 9
                       .concat(charRange(97, 97+26))  // a to b
                       .concat(charRange(65, 65+26))  // A to B
    
        var base = digits.length
    
        return function(decimal) {
                var result = ""
                while (decimal >= base) {
                    result = digits[decimal % base] + result
                    decimal = parseInt(decimal / base)
                }
                result = digits[decimal] + result
                return result
            }
    }
    
    var convert = createConverter()
    convert(parseInt(Date.now() / 1000)) // returns a 6-digit alphanumeric string 
    
  3. 我刚刚在KitKat设备上对此进行了快速测试,并且自定义句柄就在那里。

    更新:我在KitKat手机上使用横向模式复制了问题。但是,该错误不会影响所有设备。特别是,当我在平板电脑上以横向模式运行代码时,自定义句柄工作正常。

    附加观察:我测试过的EditText也有一个背景可绘制的。这种绘图在手机和平​​板电脑上都可以在纵向和横向模式下显示。但是,如果我实际在手机上以横向模式编辑该字段,则自定义背景将消失,并且该字段具有纯白色背景,直到我退出编辑模式。

    猜想:在小型设备上以横向模式编辑字段时,看起来Android会运行不同的代码路径(忽略自定义格式)。当以横向模式显示软键盘时,不会留下太多空间来显示正在电话上编辑的字段。因此,开发人员可能会在某些时候做出妥协(最好为用户显示一个非常纯文本字段进行编辑,而不是让整个字段模糊的风险)。没有深入研究源代码,这是我对这里发生的事情的最好猜测......

答案 2 :(得分:2)

以下代码适用于Nougat

filepath.Walk

答案 3 :(得分:2)

你们这些代码只能在Portrait中工作(我的也是如此)。我的应用程序只是风景,我环顾四周,看起来这是一个操作系统错误。我通知了谷歌。然而,如果还有使用EditText的解决方法,那么50分仍然可以用来获取。

答案 4 :(得分:2)

将XML属性添加到EditText

android:textColorHighlight="@color/accent_translucent"

答案 5 :(得分:2)

尝试添加

<item name="colorControlActivated">@drawable/small_rect</item>

到你的风格