指定した位置の文字列を選択

広告

テキストボックス内で既に入力された文字列の一部を選択状態にする方法を確認します。「EditText」クラスで用意されている「setSelection」メソッドを使います。(前のページで使った同名メソッドの引数が異なるメソッドです)。

Set the selection anchor to start and the selection edge to stop.

Parameters:
  start  選択開始位置
  stop  選択終了位置

1番目の引数に選択を行う開始位置をint型の値で指定します。そして2番目の引数に選択の終了位置をint型の値で指定します。最初の文字の前の位置が「0」となります。

具体的には次のように記述します。

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class Test extends Activity {
    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        EditText edit = new EditText(this);
        edit.setHeight(50);
        setContentView(edit);

        edit.setSelection(0, 3);
    }
}

全てを選択する

位置を指定して選択するのとは別に、入力された全ての文字列を選択するメソッドも用意されています。「EditText」クラスで用意されている「selectAll」メソッドを使います。

Select the entire text.

現在入力されている文字列を全て選択状態にします。

具体的には次のように記述します。

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class Test extends Activity {
    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        EditText edit = new EditText(this);
        edit.setHeight(50);
        setContentView(edit);

        edit.selectAll();
    }
}

サンプルプログラム

それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。

Test07_01.java

package jp.javadrive.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView.BufferType;

public class Test07_01 extends Activity {
    private final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);

        TextView tv = new TextView(this);
        tv.setText("What's your name?");
        linearLayout.addView(tv, 
          new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));

        final EditText edit = new EditText(this);
        edit.setWidth(200);
        edit.setText("Takahashi", BufferType.NORMAL);
        linearLayout.addView(edit, 
          new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));

        Button button1 = new Button(this);
        button1.setText("select 3 to 6 text");
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                edit.setSelection(3, 6);
            }
        });
        linearLayout.addView(button1, 
          new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));

        Button button2 = new Button(this);
        button2.setText("select all text");
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                edit.selectAll();
            }
        });
        linearLayout.addView(button2, 
          new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
    }
}

ビルド後にエミュレーター上で実行します。

指定した位置の文字列を選択

ボタンが2つ設置されています。上のボタンをクリックすると3番目から6番目の文字を選択します。

指定した位置の文字列を選択

下のボタンをクリックすると入力されている全ての文字を選択します。

指定した位置の文字列を選択

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。