入力された値を取得

広告

テキストボックスに入力された文字列を取得します。「EditText」クラスで用意されている「getText」メソッドを使います。

Return the text the TextView is displaying. If setText() was called with
an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast
the return value from this method to Spannable or Editable, respectively.

Returns:
  入力された値

戻り値としてテキストボックスに入力された値を「Editable」インターフェースを実装したクラスのオブジェクトとして取得します。

「Editable」インターフェースを実装したクラスとしては「android.text.SpannableStringBuilder」クラスがあります。「SpannableStringBuilder」クラスは文字列などを内部に持つバッファークラスです。現在のバッファーの内容を文字列として取得するには「SpannableStringBuilder」クラスで用意されている「toString」メソッドを使います。

Return a String containing a copy of the chars in this buffer.

Returns:
 バッファーに格納された内容を文字列として取得

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

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

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

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

        SpannableStringBuilder sb = (SpannableStringBuilder)edit.getText();
        String str = sb.toString();
    }
}

TextViewクラスの「getText」メソッド

テキストボックスに入力された文字列を取得するには「EditText」クラスの親クラスである「TextView」クラスにもメソッドが用意されています。で用意されている「getText」メソッドを使います。

Return the text the TextView is displaying. If setText() was called with 
an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast 
the return value from this method to Spannable or Editable, respectively.

Returns:
  入力された値

こちらのメソッドの場合は戻り値として「CharSequence」インターフェースを実装したクラスのオブジェクトとして取得します。単に文字列として取得したい場合にはこちらを使って下さい。

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

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

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

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

        CharSequence str = edit.getText();
    }
}

サンプルプログラム

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

Test05_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.app.AlertDialog;
import android.text.SpannableStringBuilder;

public class Test05_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);
        linearLayout.addView(edit, 
          new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));

        Button button1 = new Button(this);
        button1.setText("Get Text");
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                AlertDialog.show(Test05_01.this, "Input Name", 
                  edit.getText(), "ok", false);
            }
        });
        linearLayout.addView(button1, 
          new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));

        Button button2 = new Button(this);
        button2.setText("Get Text");
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                SpannableStringBuilder sb = (SpannableStringBuilder)edit.getText();
                AlertDialog.show(Test05_01.this, "Input Name", 
                  sb.toString(), "ok", false);
            }
        });
        linearLayout.addView(button2, 
          new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
    }
}

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

入力された値を取得

表示された画面でテキストボックスに何か文字列を入力して下さい。その後で2つあるボタンのどちらかをクリックして下さい。入力された値を持つダイアログが表示されます。2つのボタンは入力された値を取得するために使用するメソッドが異なるだけで結果は同じです。

入力された値を取得

入力された値を取得

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

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