そこで、HoloColorsGeneratorとActionBarStyleGeneratorを使用してHoloThemeのスタイルを自分の色に変更しました。しかし、編集テキスト内のテキストを選択すると、選択した位置の「マーカー」はまだ青色のままです。どうすれば変更できますか?
7 に答える
ここでの最悪の部分は、このアイテムの「名前」と、テーマ内での名前の付け方を見つけることでした。そこで、Android SDKフォルダー内のすべてのドローアブルを調べて、最終的に「text_select_handle_middle」、「text_select_handle_left」、「text_select_handle_right」という名前のドローアブルを見つけました。
したがって、解決策は簡単です。カスタマイズされたデザイン/色のこれらのドローアブルをドローアブルフォルダに追加し、次のようにテーマスタイル定義に追加します。
<style name="MyCustomTheme" parent="@style/MyNotSoCustomTheme">
<item name="android:textSelectHandle">@drawable/text_select_handle_middle</item>
<item name="android:textSelectHandleLeft">@drawable/text_select_handle_left</item>
<item name="android:textSelectHandleRight">@drawable/text_select_handle_right</item>
</style>
コードからそれを行う方法:
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);
final Resources res = context.getResources();
fSelectHandleLeft.set(editor, res.getDrawable(R.drawable.text_select_handle_left));
fSelectHandleRight.set(editor, res.getDrawable(R.drawable.text_select_handle_right));
fSelectHandleCenter.set(editor, res.getDrawable(R.drawable.text_select_handle_middle));
} catch (final Exception ignored) {
}
これは本当に遅いと思いますが、ハンドルの色を変更するだけの場合は、styles.xmlファイルに次を追加するだけです。
<style name="ColoredHandleTheme">
<item name="colorControlActivated">@color/colorYouWant</item>
</style>
EditText
そして、あなたが影響を与えたいと思っている活動が何であれ、テーマを設定するだけです。
または、アプリ全体に設定する場合は、次の操作を実行できます。
<style name="ColoredHandleThemeForWholeApp">
<item name="colorAccent">@color/colorYouWant</item>
</style>
そして、アプリ全体にそのテーマを設定します。
問題が解決しました!
選択したハンドルの色を変更するには、アプリのテーマでアクティブになっている色を上書きする必要があります。
<style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:colorControlActivated">@color/customActivatedColor</item>
</style>
これらの属性はhttp://androiddrawables.com/Other.htmlで確認できます。
たとえば、values/styles.xmlを変更します。
<style name="AppTheme.Cursor" parent="AppTheme">
<item name="colorAccent">@color/cursor</item>
</style>
ここで、@ color/cursorはvalues/color.xmlに追加されます。その後、スタイルをアクティビティに適用します。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme_Cursor);
...
他の解決策については、(カーソルではなく) EditTextポインターの色を変更する方法を参照してください。
コードからこれを処理しようとする人のために、28以下のAPIバージョンに対してJared Rummlerによって提供された回答を使用できますが、Android Q(API 29)から、@ UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P )アノテーションがあり、Q+では機能しないようです。代わりに、EditTextに追加された新しいメソッド:
setTextSelectHandle(R.drawable.test);
setTextSelectHandleLeft(R.drawable.test);
setTextSelectHandleRight(R.drawable.test);
したがって、それを機能させるには、次のようなことを行うことができます。
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) {
}
}
1つのEditTextでのみポインターを非表示にする場合は、それにのみテーマを設定できます。
styles.xml
<style name="TransparentPointerEditText">
<item name="colorControlActivated">@android:color/transparent</item>
</style>
利用方法
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/TransparentPointerEditText" />