我很难自定义EditText选择句柄。我正在关注此线程:
看起来很简单。但是,我无法在景观环境中使用它。谁能发现我在做什么错?我几乎在测试活动中粘贴了相同的代码,但锚点句柄始终相同。我尝试按照建议和编程方式使用样式。仍然我总是得到相同的默认蓝色锚点:(
我在牛轧糖上不确定是否会有所作为。
测试活动:
@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>
可绘制对象(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>
结果:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句