Androidチェックボックスの例
Androidでは、 “http://developer.android.com/reference/android/widget/CheckBox.html[android.widget.CheckBox]”クラスを使用してチェックボックスをレンダリングできます。
このチュートリアルでは、XMLファイルに3つのチェックボックスを作成する方法を示し、チェックボックスの状態をチェックするためにリスナーを使用する方法を示します(オンまたはオフ)。
P.SこのプロジェクトはEclipse 3.7で開発され、Android 2.3.3.
でテストされています
1.カスタム文字列
”
res/values/strings.xml
“ファイルを開き、ユーザ定義の文字列を追加します。
File:res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MyAndroidAppActivity!</string>
<string name="app__name">MyAndroidApp</string>
<string name="chk__ios">IPhone</string>
<string name="chk__android">Android</string>
<string name="chk__windows">Windows Mobile</string>
<string name="btn__display">Display</string>
</resources>
2. CheckBox
”
res/layout/main.xml
“ファイルを開き、 “LinearLayout”の中に3つの ”
CheckBox
“とボタンを追加します。
ファイル:res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout__width="fill__parent"
android:layout__height="fill__parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/chkIos"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="@string/chk__ios"/>
<CheckBox
android:id="@+id/chkAndroid"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="@string/chk__android"
android:checked="true"/>
<CheckBox
android:id="@+id/chkWindows"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="@string/chk__windows"/>
<Button
android:id="@+id/btnDisplay"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="@string/btn__display"/>
</LinearLayout>
3.コードコード
あなたのアクティビティの “ onCreate() `”メソッドの中にリスナを付けて、以下のイベントを監視します:
-
チェックボックスID: ”
chkIos
“がチェックされている場合は、
メッセージ “Bro、Androidを試してください”。
-
ボタンをクリックするとフローティングボックスが表示され、
チェックボックスの状態。
ファイル:私のAndroid App Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MyAndroidAppActivity extends Activity {
private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnChkIos();
addListenerOnButton();
}
public void addListenerOnChkIos() {
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkIos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
Toast.makeText(MyAndroidAppActivity.this,
"Bro, try Android :)", Toast.LENGTH__LONG).show();
}
}
});
}
public void addListenerOnButton() {
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
chkWindows = (CheckBox) findViewById(R.id.chkWindows);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
//Run when button is clicked
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("IPhone check : ").append(chkIos.isChecked());
result.append("\nAndroid check : ").append(chkAndroid.isChecked());
result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
Toast.makeText(MyAndroidAppActivity.this, result.toString(),
Toast.LENGTH__LONG).show();
}
});
}
}
4.デモ
アプリケーションを実行します。
{空} 1。結果:

{空} 2。 「IPhone」がチェックされている場合:

{空} 3。 「IPhone」と「Windows Mobile」をチェックした後、「表示」ボタンをクリックします:

ソースコードをダウンロードする
ダウンロードする –
Android-Checkbox-Example.zip
(15 KB)
参考文献
CheckBox JavaDoc]。
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#Checkbox
[Android
CheckBoxの例]
リンク://タグ/アンドロイド/[アンドロイド]リンク://タグ/チェックボックス/[チェックボックス]