| 2012/01/23 | スピナー について | Android |
| 2011/12/30 | レーティングバー/Form Stuff について | Android |
| 2011/12/23 | トグルボタン/Form Stuff について | Android |
| 2011/12/18 | ラジオボタン/Form Stuff について | Android |
| 2011/12/05 | チェックボックス/Form Stuff について | Android |
| 2011/11/22 | 編集テキスト(入力ボックス)/Form Stuff について | Android |
| 2011/11/15 | カスタムボタン/Form Stuff について | Android |
| 2011/09/11 | Time Picker について | Android |
| 2011/08/31 | Date Picker について | Android |
| 2011/08/17 | List View について | Android |
| スポンサードリンク | ||
|
|
スピナー について
Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。
- Date Picker
- Time Picker
- Form Stuff
- Spinner
- Auto Complete
- Gallery
「Spinner」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。
Spinner
Spinnerの説明についてみていきます。
Spinner is a widget similar to a drop-down list for selecting items.
In this tutorial, you'll create a simple spinner widget that displays a list of planets. When one is selected, a toast message will display the selected item.
[訳]
Spinnerは、項目を選択するためのドロップダウンリストに似たウィジットです。
このチュートリアルでは、惑星のリストを表示する簡単なスピナーウィジットを作ります。1つが選択されたときに、トーストメッセージでその選択項目を表示します。

|
|
レーティングバー/Form Stuff について
Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。
- Date Picker
- Time Picker
- Form Stuff
L Custom Button
L Edit Text
L Checkbox
L Radio Buttons
L Toggle Button
L Rating Bar - Spinner
- Auto Complete
- Gallery
「Form Stuff」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。今回はRating Barについてみていきます。
Rating Bar
Rating Barの説明についてみていきます。
RatingBar widget.[訳]
このセクションではRatingBar ウィジットを使って、ユーザがレーティングできるウィジットを作ります。

|
|
トグルボタン/Form Stuff について
Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。
- Date Picker
- Time Picker
- Form Stuff
L Custom Button
L Edit Text
L Checkbox
L Radio Buttons
L Toggle Button
L Rating Bar - Spinner
- Auto Complete
- Gallery
「Form Stuff」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。今回はToggle Buttonについてみていきます。
Toggle Button
Radio Buttonsの説明についてみていきます。
ToggleButton widget. This widget is an excellent alternative to radio buttons if you have two simple states that are mutually exclusive ("on" and "off", for example).[訳]
このセクションではToggleButton ウィジットを使って、特に2つの状態を切り替えるトグルに使われるボタンを作ります。このウィジットは、互いに排他的な2つの単純な状態(例えば"on"と"off")を持っているときにラジオボタンの変わりとして使うには優秀です。
|
|
ラジオボタン/Form Stuff について
Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。
- Date Picker
- Time Picker
- Form Stuff
L Custom Button
L Edit Text
L Checkbox
L Radio Buttons
L Toggle Button
L Rating Bar - Spinner
- Auto Complete
- Gallery
「Form Stuff」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。今回はRadio Buttonsについてみていきます。
Radio Buttons
Radio Buttonsの説明についてみていきます。
RadioGroup and RadioButton widgets. When either radio button is pressed, a toast message will be displayed.[訳]
このセクションでは、RadioGroupとRadioButtonのウィジットを使って2つの相互に排他的なラジオボタン(1つが可能ならば他方が無効)を作ります。どちらかのラジオボタンが押されたときに、トーストメッセージを表示します。
|
|
チェックボックス/Form Stuff について
Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。
- Date Picker
- Time Picker
- Form Stuff
L Custom Button
L Edit Text
L Checkbox
L Radio Buttons
L Toggle Button
L Rating Bar - Spinner
- Auto Complete
- Gallery
「Form Stuff」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。今回はCheckboxについてみていきます。
CheckBox
CheckBoxの説明についてみていきます。
CheckBox widget. When the checkbox is pressed, a toast message will indicate the current state of the checkbox.[訳]
このセクションでは、CheckBox ウィジットを使ってユーザが選択できるチェックボックスを作ります。チェックボックスが押されたときに、チェックボックスの現在の状態を示すトーストメッセージが表示されます。
AndroidチュートリアルではCustom Button、Edit Text、Checkbox、Radio Buttons、Toggle Button、Rating Barは1つのプロジェクト内で説明していますが、このブログでは各フォーム要素ごとにプロジェクトを作成します。
FormStuffCheckBoxという名前の新規プロジェクトを作成します。
- プロジェクト名(Project name)
FormStuffCheckBox - 内容(Contents)
ワークスペース内に新規プロジェクトを作成 - ビルド・ターゲット(Build Target)
Android 1.6 - アプリケーション名(Application Name)
FormStuffCheckBox - パッケージ名(Package Name)
localhost.helloview - アクティビティの作成(Create Activity)
FormStuffCheckBox - 最小 SDKバージョン(Min SDK Version)
4
プロジェクト作成については「新規 Android プロジェクトを作成する」の記事に詳しく書いたので参考にしてください。パッケージ名については公開するつもりもないので、とりあえずローカル環境用という意味合いで「localhost」にしました。
[訳]
res/layout/main.xml ファイルを開き、CheckBox要素を追加します(LinearLayoutの中に)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check it out" />
</LinearLayout>
※チュートリアルではエレメントの追加部分だけ記載されていましたが、この記事ではres/layout/main.xml の全コードを記載しておきます。
onCreate() method:[訳]
状態が変化したときに何らかの処理をするため、次のコードをonCreate()メソッドの後ろに追加します。
package localhost.helloview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
public class FormStuffCheckBox extends Activity {
/** アクティビティが最初に作られた時に呼ばれます */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//レイアウトからCheckBoxインスタンス生成
final CheckBox checkbox
= (CheckBox) findViewById(R.id.checkbox);
//クリックイベントハンドラを付与
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//チェックされている場合
if (((CheckBox) v).isChecked()) {
Toast.makeText(
FormStuffCheckBox.this,
"Selected",
Toast.LENGTH_SHORT).show();
//チェックされていない場合
}else{
Toast.makeText(
FormStuffCheckBox.this,
"Not selected",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
※チュートリアルでは追加部分だけ記載されていましたが、この記事ではFormStuffCheckBox.java の全コードを記載しておきます。
CheckBox element from the layout, then adds an View.OnClickListener. The View.OnClickListener must implement the onClick(View) callback method, which defines the action to be made when the checkbox is clicked. When clicked, isChecked() is called to check the new state of the check box. If it has been checked, then a Toast displays the message "Selected", otherwise it displays "Not selected". Note that the View object that is passed in the onClick(View) callback must be cast to a CheckBox because the isChecked() method is not defined by the parent View class. The CheckBox handles its own state changes, so you only need to query the current state.[訳]
ここではレイアウトからCheckBox エレメントを取得し、View.OnClickListenerを付与します。OnClickListenerはonClick(View)コールバックメソッドを実装しなければなりません。onClickメソッドにはチェックボックスがクリックされたときに処理されるアクションを定義します。クリックしたときに、新しいチェックの状態をチェックするためisChecked()メソッドが呼ばれます。もしチェックされている状態ならばトーストには"Selected"というメッセージが表示され、そうでなければ"Not selected"と表示されます。onClick(View)コールバックで渡されるViewオブジェクトはCheckBoxにキャストしなければなりません。なぜならisChecked()は親のViewクラスには定義されていないからです。CheckBoxは自分自身の状態変化をハンドルします。なので現在の状態を尋ねるだけでよくなります。
- 18行目でレイアウトのIDがcheckboxの要素から
CheckBoxインスタンスを生成します。 - 21行目で
CheckBoxインスタンスにをView.OnClickListenerを付与しています。OnClickListenerはインターフェースなので実装する場合はonClickメソッドをオーバーライドしなければなりません。
※参考:android.view.OnClickListener - 22行目でonClickメソッドをオーバーライドしています。onClickメソッドはViewがクリックされたときに呼ばれます。パラメータとして渡されるのはクリックされたViewインスタンスです。
- 24行目でチェックボックスがチェック状態であるか判定しています。パラメータとして渡されたvはViewクラス型なのでisCheckedメソッドは定義されていません。そこでメソッドが定義されているCheckBoxクラス型にキャストします。CheckBoxクラスのリファレンスをみるとこのクラス自体が定義しているわけではないようです。ViewからChecBoxまでには次のようなクラス階層があります。
android.view.View L android.widget.TextView L android.widget.Button L android.widget.CompoundButton L android.widget.CheckBoxCheckBoxのスーパークラスであるCompoundButtonクラスで実装されおり、それを継承してます。
- 23~28行目がチェックボックスがチェックされたときの処理が、29行目から35行目はチェックが外れたときの処理が書かれています。それぞれに応じたメッセージがトーストで表示されます。
AVDによるアプリケーションの実行については「AVDでアプリケーションを実行する」の記事で詳しく書きました。参考にしてください。
実行結果をみてみます。次のような画面が表示されました。
