ビュー
■Viewの取得
TextView tv = (TextView)findViewById(R.id.textView1);
■レイアウトの切り替え
レイアウト画面の「Outline」から「RelativeLayout」を右クリックし、「ChangeLayout」で「LinearLayout(Vertical)」を選択。
■Viewクラス
全てのビューの基底クラス
属性
| id | 識別子(@+id/文字列) |
| backgroud | 背景(色コードなど) |
| layout_width | 幅(match_parent または wrap_content) |
| layout_height | 高さ(match_parent または wrap_content) |
| padding | パディング(枠内の余白) 単位 dp |
| layout_margin | マージン(枠外の余白) 単位 dp |
■TextView
http://developer.android.com/reference/android/widget/TextView.html属性
| text | 表示文字列 |
| textSize | 文字サイズ 例"20sp" |
| textColor | 文字色 例 "#ff0000" |
メソッド
| CharSequence getText() | 表示文字列の取得 |
| void setText(CharSequence text) | 表示文字列の設定 |
■EditText
http://developer.android.com/reference/android/widget/EditText.html
文字の編集が可能なTextView
※TextViewを継承
属性
| hint | ヒント |
| inputType | 入力文字 文字 text,数字 number,パスワード textPassword |
メソッド
| Editable getText() | 編集文字列の取得 |
■Button
http://developer.android.com/reference/android/widget/Button.html
※TextViewを継承
■CheckBox
http://developer.android.com/reference/android/widget/CheckBox.html
※Buttonを継承
属性
| checked | チェックされているか(true/false) |
メソッド
| void setChecked(boolean checked) | チェック状態の設定 |
| boolean isChecked() | チェック状態の取得 |
■RadioButton/RadioGroup
http://developer.android.com/reference/android/widget/RadioButton.html
http://developer.android.com/reference/android/widget/RadioGroup.html
※Buttonを継承
単一選択。RadioGroup中にRadioButtonを入れる
RadioGroup メソッド
| int getCheckedRadioButtonId() | 選択されているRadioButtonのidを得る |
| check(int id) | idのRadioButtonを選択する |
■ImageView
http://developer.android.com/reference/android/widget/ImageView.html
属性
maxWidth 最大幅 maxHeight 最大高さ src 画像リソースIDメソッド
| void setImageResource(int resId) | 画像リソースID |
| void setAlpha(int alpha) | 透明度の指定 0(透明)~255(不透明) |
■Spinner
http://developer.android.com/reference/android/widget/Spinner.html
複数のものから一つを選択。初期表示単一(コンボボックス)
選択肢の定義(/res/values/strings.xml内)
メソッド
| Object getSelectedItem() | 選択アイテムの取得 |
| int getSelectedItemPosition() | 選択位置の取得 |
■ListView
http://developer.android.com/reference/android/widget/ListView.html
複数のものから一つを選択。初期表示複数(リストボックス)
Adapterクラスを利用し、選択肢を設定。 選択したものを取得する。
final TextView tv = (TextView)findViewById(R.id.textView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
adapter.add("犬");
adapter.add("ネコ");
adapter.add("うさぎ");
ListView listview = (ListView)findViewById(R.id.listView1);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView listView = (ListView) parent;
String item = (String) listView.getItemAtPosition(position);
tv.setText(item);
}
});
Android開発 虎の巻