EditTextを作成し、次のコードのようにフォーマットを含むテキストを追加しました。
EditText richTextView = (EditText)findViewById(R.id.rich_text);
// this is the text we'll be operating on
SpannableStringBuilder text = new SpannableStringBuilder("Lorem ipsum dolor sit amet");
// make "Lorem" (characters 0 to 5) red
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);
// make "ipsum" (characters 6 to 11) one and a half time bigger than the textbox
text.setSpan(new RelativeSizeSpan(1.5f), 6, 11, 0);
// make "dolor" (characters 12 to 17) display a toast message when touched
final Context context = this;
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
Toast.makeText(context, "dolor", Toast.LENGTH_LONG).show();
}
};
text.setSpan(clickableSpan, 12, 17, 0);
// make "sit" (characters 18 to 21) struck through
text.setSpan(new StrikethroughSpan(), 18, 21, 0);
// make "amet" (characters 22 to 26) twice as big, green and a link to this site.
// it's important to set the color after the URLSpan or the standard
// link color will override it.
text.setSpan(new RelativeSizeSpan(2f), 22, 26, 0);
text.setSpan(new ForegroundColorSpan(Color.GREEN), 22, 26, 0);
// make our ClickableSpans and URLSpans work
richTextView.setMovementMethod(LinkMovementMethod.getInstance());
// shove our styled text into the TextView
richTextView.setText(text, BufferType.EDITABLE);
私の問題は、プログラムの実行時にテキストを選択できないことです(エミュレーターと自分のデバイスの両方で試しました)。「dolor」以外の単語をクリックするとカーソルは表示されませんが、入力すると「Lorem」の前から入力を開始します。ただし、「dolor」をクリックすると、単語が選択されて置換できます(ただし、置換せずに入力することはできません)。
単語の他の部分を選択できず、カーソルを目的の場所に置くことができません(表示されません)。
SpannableStringまたはSpannableStringBuilderの代わりに通常のテキストを使用するときに存在するような通常のテキスト選択機能をどのように取得できるのでしょうか。(両方試してみました)プレーンテキストを使用すれば、任意の単語の任意の部分を選択して、そこからテキストの入力を開始できます。
Edit1 テキスト選択は横向きモードでは機能しますが、縦向きでは機能しません。したがって、コードはある程度有効です...
Edit2 実際、テキスト選択は私の電話では横向きモードで機能しますが、エミュレーターでは機能しません。