オブジェクトの配置

このページは、レイアウト用ファイルに記述するコード例をまとめています。

TextView

指定した文字列を表示するのに使います。

コード例

<TextView
android:layout_width="wrap_content"     …①
android:layout_height="wrap_content"    …①
android:layout_marginTop="5dp"           …②
android:layout_marginBottom="5dp"        …②
android:text="これはTextViewです"       …③

/>

補足

  • ①は、
    • wrap_content : 指定した文字列のサイズにする
    • fill_parent : 画面端まで
  • ②は、省略可能です。ピクセルサイズを指定します。
  • ③は、直接指定する例です。string.xmlを使う場合は
    • android:text="@string/abcdefg"
      • のように書くのですが、このときオレンジ色の文字列とstring.xmlの記述
      • <string name="abcdefg">これはTextViewです</string>
      • をあわせてください。

EditText

文字を入力するフィールドを作成します。

コード例

<EditText
android:id="@+id/edittext"                 …①
android:layout_width="fill_parent"         …②
android:layout_height="wrap_content"       …③
android:hint="薄いグレーの文字"              …④
android:selectAllOnFocus="true"              …⑤

補足

  • ①は省略できます。ただ、実際は入力された文字列をプログラムで使うでしょうから、そのときは①は省略してはいけません。
    • 青文字は、入力された文字列を取り出すときに使う識別子になりますので、適切につけましょう。
  • ②、③はTextViewの説明をみてください。
  • ④は、文字列が入力されていないときに薄く表示される文字列を指定します(なくてもいいです)
  • ⑤は、入力フィールドが選択されたときに、入力済み文字列を選択するか否かを指定します(なくてもいいです)

文字列の取得方法

EditText txtmsg = (EditText) findViewById(R.id.edittext);

String msg = txtmsg.getText().toString();

※表示させるだけなら、txtmsg.getText()でいいが、この出力はStringタイプでないため、

Stringとして処理させたい場合は、上記のようにする。

CheckBox

チェックボックスを作成します。

コード例

<CheckBox

android:id="@+id/checkbox"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

補足はありません。TextViewやEditTextの説明を参照してください。

Button

ボタンを作成します。

コード例

<Button

android:id="@+id/button"

android:text="ボタン"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

RadioButton

ラジオボタンを作成します。

コード例

<RadioButton

android:id="@+id/radiobutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/radiobutton" />

補足

  • 通常ラジオボタンは、複数の選択項目の中からどれか1つを選ぶのに使います。なので、通常は
    • <RadioGroup
      • android:layout_width="fill_parent
      • android:layout_height="wrap_content"
      • android:orientation="vertical">
    • *** 複数の <RadioButton ... /> タグの定義
    • </RadioGroup>
    • のように使います。

Spinner

選択ボックスを作成します。

コード例

<Spinner

android:id="@+id/spinner"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:entries="@array/spinner_list" />

補足

  • 選択用の文字リストは、string.xmlの中に定義します。
    • <string-array name="spinner_list">
      • <item>あ</item>
      • <item>い</item>
      • :
    • </string-array>

選択された文字列の取得方法

  1. リスナーを定義する
  2. リスナーの中に以下のようなコードを書く
    1. Spinner list = (Spinner)findViewById(R.id.spinner_list);
    2. String item = (String)list.getSelectedItem();
    3. ※spinner_listは、strings.xmlの中に定義したものにあわせる

ListView

選択可能な一覧を作成します。

コード例

<ListView

android:id="@+id/listview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:entries="@array/listview_list" />

補足はありません。

選択された文字列の取得方法

  1. ActivityクラスのonCreateメソッドの中に以下を記述して、リストをタッチしたときに「実行したい処理」が
    1. 呼ばれるようにします。
    2. ListView listview = (ListView)findViewById(R.id.listview1);
    3. listview.setOnItemClickListener(new ListItemClickListener());
    4. 青字は、ユーザーが決める名前です。R.id.listview1の青字は、string.xmlのListViewのid設定とあわせます。
    5. 赤字は、ユーザーが決めるclass名です。
  2. 赤字のclassを定義します。赤字は1番の赤字と同じになります。青字はユーザーが決めるところ。
    1. class ListItemClickListener implements OnItemClickListener {
      1. public void onItemClick (
        1. AdapterView<?> parent,
        2. VIew view,
        3. int position,
        4. long id)
      2. ){
        1. ListView listview = (ListView) parent;
        2. String item = (String) listview.getItemAtPosition(position);
        3. (ユーザーの処理したいことを書く)
      3. }
    2. }