コピー/貼り付け機能を実装しようとしています。EditText からテキストの選択を取得するにはどうすればよいですか?
EditText et=(EditText)findViewById(R.id.title);
ボタンの blabla onclicklistener:
int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();
それから私は立ち往生しています。何か案は?
コピー/貼り付け機能を実装しようとしています。EditText からテキストの選択を取得するにはどうすればよいですか?
EditText et=(EditText)findViewById(R.id.title);
ボタンの blabla onclicklistener:
int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();
それから私は立ち往生しています。何か案は?
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.
Editable オブジェクトの特別な関数を使用する必要があります。
Editable txt = et.getText();
txt.replace(int st, int en, CharSequence source)
このコマンドは、(st..en) で指定された部分を文字列 (CharSequence) に置き換えます。
このすべてを行う必要はありません。編集テキストを長押しするだけで、コピー/貼り付け/選択などに関連するすべてのオプションが表示されます。テキストを保存する場合は、mbaird が示す方法を使用します。
String selectedText = et.getText().toString().substring(startSelection, endSelection);
getText() は編集可能なものを返します。部分文字列には文字列が必要です。toString() はそれらを適切に接続します。