Android-checkbox-control
Android-チェックボックスコントロール
'_CheckBoxは、ユーザーが切り替えることができるオン/オフスイッチです。 相互に排他的ではない選択可能なオプションのグループをユーザーに提示するときは、チェックボックスを使用する必要があります。_
チェックボックス
チェックボックスの属性
以下は、CheckBoxコントロールに関連する重要な属性です。 属性の完全なリストと、これらの属性を実行時に変更するために使用できる関連メソッドについては、Androidの公式ドキュメントを確認してください。
*android.widget.TextView* クラスから継承-
Sr.No | Attribute & Description |
---|---|
1 |
android:autoText 設定されている場合、このTextViewにテキスト入力メソッドがあり、いくつかの一般的なスペルエラーを自動的に修正することを指定します。 |
2 |
android:drawableBottom これは、テキストの下に描画されるドロアブルです。 |
3 |
android:drawableRight これは、テキストの右側に描画されるドロアブルです。 |
4 |
android:editable 設定されている場合、このTextViewに入力メソッドがあることを指定します。 |
5 |
android:text これは表示するテキストです。 |
*android.view.View* クラスから継承-
Sr.No | Attribute & Description |
---|---|
1 |
android:background これは背景として使用するドロアブルです。 |
2 |
android:contentDescription これは、ビューのコンテンツを簡単に説明するテキストを定義します。 |
3 |
android:id これは、このビューの識別子名を提供します。 |
4 |
android:onClick これは、ビューがクリックされたときに呼び出すこのビューのコンテキスト内のメソッドの名前です。 |
5 |
android:visibility これにより、ビューの初期可視性が制御されます。 |
例
この例では、簡単な手順で、Linear LayoutとCheckBoxを使用して独自のAndroidアプリケーションを作成する方法を示します。
Step | Description |
---|---|
1 | You will use Android Studio IDE to create an Android application and name it as myapplication under a package com.example.myapplication as explained in the Hello World Example chapter. |
2 | Modify src/MainActivity.java file to add a click event. |
3 | Modify the default content of res/layout/activity_main.xml file to include Android UI control. |
4 | No need to declare default string constants. Android studio takes care of default constants at string.xml |
5 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル src/MainActivity.java の内容です。 このファイルには、基本的な各ライフサイクルメソッドを含めることができます。
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends Activity {
CheckBox ch1,ch2;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch1=(CheckBox)findViewById(R.id.checkBox1);
ch2=(CheckBox)findViewById(R.id.checkBox2);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("Thanks : ").append(ch1.isChecked());
result.append("\nThanks: ").append(ch2.isChecked());
Toast.makeText(MainActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
以下は res/layout/activity_main.xml ファイルの内容です-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of checkbox"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you like Tutorials Point"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you like android "
android:checked="false"
android:layout_above="@+id/checkBox1"
android:layout_alignLeft="@+id/checkBox1"
android:layout_alignStart="@+id/checkBox1"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/textView1"
android:layout_marginTop="39dp"
android:text="Tutorials point"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_alignRight="@+id/textView1"
android:layout_alignEnd="@+id/textView1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/checkBox1"
android:layout_alignStart="@+id/checkBox1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
以下は、これらの新しい定数を定義する res/values/strings.xml の内容です-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyApplication</string>
</resources>
以下は、 AndroidManifest.xml のデフォルトのコンテンツです-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
*MyApplication* アプリケーションを実行してみましょう。 環境設定中に *AVD* を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
ユーザーは、Androidチェックボックスが好きですか、チュートリアルポイントチェックボックスが好きですかをオンにする必要があります。 [OK]ボタンを押します。すべての処理が正しく行われると、感謝のメッセージが表示されます。 または、キャンセルボタンを押します。ユーザーがキャンセルボタンを押すと、アプリケーションが閉じます。
運動
プログラミング時にレイアウトXMLファイルのCheckBoxのさまざまな属性を使用して上記の例を試して、CheckBoxのルックアンドフィールを変えることをお勧めします。 編集可能にし、フォントの色、フォントファミリ、幅、textSizeなどに変更して、結果を確認してください。 1つのアクティビティで複数のCheckBoxコントロールを使用して上記の例を試すこともできます。