スポンサードリンク

新着記事
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/11Time Picker についてAndroid
2011/08/31Date Picker についてAndroid
2011/08/17List View についてAndroid

スピナー について

2012 年 1 月 23 日 月曜日  【 カテゴリー: Android


Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。

  1. Date Picker
  2. Time Picker
  3. Form Stuff
  4. Spinner
  5. Auto Complete
  6. 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 について

2011 年 12 月 30 日 金曜日  【 カテゴリー: Android


Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。

  1. Date Picker
  2. Time Picker
  3. Form Stuff
    L Custom Button
    L Edit Text
    L Checkbox
    L Radio Buttons
    L Toggle Button
    L Rating Bar
  4. Spinner
  5. Auto Complete
  6. Gallery

Form Stuff」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。今回はRating Barについてみていきます。

Rating Bar

Rating Barの説明についてみていきます。

In this section, you'll create a widget that allows the user to provide a rating, with the RatingBar widget.

[訳]
このセクションではRatingBar ウィジットを使って、ユーザがレーティングできるウィジットを作ります。

この投稿の続きを読む »


トグルボタン/Form Stuff について

2011 年 12 月 23 日 金曜日  【 カテゴリー: Android


Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。

  1. Date Picker
  2. Time Picker
  3. Form Stuff
    L Custom Button
    L Edit Text
    L Checkbox
    L Radio Buttons
    L Toggle Button
    L Rating Bar
  4. Spinner
  5. Auto Complete
  6. Gallery

Form Stuff」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。今回はToggle Buttonについてみていきます。

Toggle Button

Radio Buttonsの説明についてみていきます。

In this section, you'll create a button used specifically for toggling between two states, using the 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 について

2011 年 12 月 18 日 日曜日  【 カテゴリー: Android


Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。

  1. Date Picker
  2. Time Picker
  3. Form Stuff
    L Custom Button
    L Edit Text
    L Checkbox
    L Radio Buttons
    L Toggle Button
    L Rating Bar
  4. Spinner
  5. Auto Complete
  6. Gallery

Form Stuff」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。今回はRadio Buttonsについてみていきます。

Radio Buttons

Radio Buttonsの説明についてみていきます。

In this section, you will create two mutually-exclusive radio buttons (enabling one disables the other), using the RadioGroup and RadioButton widgets. When either radio button is pressed, a toast message will be displayed.

[訳]
このセクションでは、RadioGroupRadioButtonのウィジットを使って2つの相互に排他的なラジオボタン(1つが可能ならば他方が無効)を作ります。どちらかのラジオボタンが押されたときに、トーストメッセージを表示します。

この投稿の続きを読む »


チェックボックス/Form Stuff について

2011 年 12 月 5 日 月曜日  【 カテゴリー: Android


Android developers による「Hello Views」チュートリアルで紹介されているウィジットには次のようなものがあります。

  1. Date Picker
  2. Time Picker
  3. Form Stuff
    L Custom Button
    L Edit Text
    L Checkbox
    L Radio Buttons
    L Toggle Button
    L Rating Bar
  4. Spinner
  5. Auto Complete
  6. Gallery

Form Stuff」チュートリアルを日本語への翻訳と内容を補足しながらみていきます。今回はCheckboxについてみていきます。

CheckBox

CheckBoxの説明についてみていきます。

In this section, you will create a checkbox for selecting items, using the 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」にしました。

レイアウトファイルの作成(main.xml)
Open the res/layout/main.xml file and add the CheckBox element (inside the LinearLayout):

[訳]
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 の全コードを記載しておきます。

アクティビティ作成(FormStuffCheckBox.java)
To do something when the state is changed, add the following code to the end of the 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 の全コードを記載しておきます。

This captures the 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.CheckBox

     CheckBoxのスーパークラスであるCompoundButtonクラスで実装されおり、それを継承してます。

  • 23~28行目がチェックボックスがチェックされたときの処理が、29行目から35行目はチェックが外れたときの処理が書かれています。それぞれに応じたメッセージがトーストで表示されます。
AVDによる実行

AVDによるアプリケーションの実行については「AVDでアプリケーションを実行する」の記事で詳しく書きました。参考にしてください。

実行結果をみてみます。次のような画面が表示されました。

Pages: 1 2 3 4 5 6 7 8 9 10 ...59 60 61 Next