10

コピー/貼り付け機能を実装しようとしています。EditText からテキストの選択を取得するにはどうすればよいですか?

EditText et=(EditText)findViewById(R.id.title);

ボタンの blabla onclicklistener:

int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();

それから私は立ち往生しています。何か案は?

4

5 に答える 5

16

Seems like you've already done the hard part by finding what the selected area is. Now you just need to pull that substring out of the full text.

Try this:

String selectedText = et.getText().substring(startSelection, endSelection);

It's just a basic Java String operation.

于 2010-01-29T17:45:14.957 に答える
0

Editable オブジェクトの特別な関数を使用する必要があります。

Editable txt = et.getText();
txt.replace(int st, int en, CharSequence source)

このコマンドは、(st..en) で指定された部分を文字列 (CharSequence) に置き換えます。

于 2010-08-26T17:34:23.130 に答える
0

このすべてを行う必要はありません。編集テキストを長押しするだけで、コピー/貼り付け/選択などに関連するすべてのオプションが表示されます。テキストを保存する場合は、mbaird が示す方法を使用します。

于 2012-10-12T09:23:35.293 に答える
0

String selectedText = et.getText().toString().substring(startSelection, endSelection);
getText() は編集可能なものを返します。部分文字列には文字列が必要です。toString() はそれらを適切に接続します。

于 2015-01-29T05:45:40.070 に答える