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

前端 未结 6 1360
不思量自难忘°
不思量自难忘° 2020-11-27 17:46

So I changed the style of the Holo Theme with the Holo Colors Generator and Action Bar Style Generator to my own color. But when I select text inside an edit text, the \"mar

6条回答
  •  执念已碎
    2020-11-27 18:08

    For those who try to handle this from code, you can use answer provided by Jared Rummler for API version less or equal to 28, but from Android Q(API 29) they've added a @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) annotation and it doesn't seem to work on Q+. Instead a new methods where added to EditText:

    setTextSelectHandle(R.drawable.test);
    setTextSelectHandleLeft(R.drawable.test);
    setTextSelectHandleRight(R.drawable.test);
    

    So, to make it work you could do something like this:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    setTextSelectHandle(R.drawable.test);
                    setTextSelectHandleLeft(R.drawable.test);
                    setTextSelectHandleRight(R.drawable.test);
                } else {
                    try {
                        final Field fEditor = TextView.class.getDeclaredField("mEditor");
                        fEditor.setAccessible(true);
                        final Object editor = fEditor.get(this);
    
                        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);
    
                        final Resources res = getResources();
    
                        fSelectHandleLeft.set(editor, res.getDrawable(R.drawable.test, null));
                        fSelectHandleRight.set(editor, res.getDrawable(R.drawable.test, null));
                        fSelectHandleCenter.set(editor, res.getDrawable(R.drawable.test, null));
                    } catch (final Exception ignored) {
                    }
                }
    

提交回复
热议问题