Android——基本コントロールのラジオボタン:RadioGroup (7)

1.知識のポイント

(1) RadioGroupクラスとRadioButtonクラスの使い方をマスターする。

2.具体内容

HTMLでラジオボタンを使用するにはどうすればよいですか?

<html>
	<body>
		<input type="radio" name="init" vlaue="boy">男
		<input type="radio" name="init" vlaue="girl">女
	</body>
</html>

以上がHTMLでのラジオボタンの書き方です。では、Android でラジオ ボタンを実装するにはどうすればよいでしょうか?

Android では、ラジオ ボタン コンポーネントは RadioButton であり、すべての RadioButton を RadioGroup に配置する必要があります。この RadioGroup 内のすべてのラジオ ボタンのうち 1 つだけを選択できることを示します。

 RadioGroup が LinearLayout のサブクラスであることがわかり、LinearLayout 内の以前の定義はすべて RadioGroup に配置できます。

 

 

継承関係から、RadioButton が特別なボタン コンポーネントであることがわかります。

例: 性別を選択します:

<LinearLayout 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:orientation="vertical">

    <TextView
        android:id="@+id/getSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择性别:" />
    <RadioGroup
        android:layout_width="match_parent"
    	android:layout_height="match_parent"
		android:orientation="vertical"    
        >
        <!-- 这里就是放置RadioButton -->
        <RadioButton 
            android:checked="true"—默认选中
            android:id="@+id/boy"
            android:text="男"
            />
        <RadioButton 
            android:id="@+id/girl"
            android:text="女"
            />
    </RadioGroup>
</LinearLayout>

現在のコンテンツは縦に積み重ねていますが、もちろん横に積み重ねることもできます。

<RadioGroup
        android:layout_width="match_parent"
    	android:layout_height="match_parent"
		android:orientation="horizontal"    --设置水平码放
        >
        <!-- 这里就是放置RadioButton -->
        <RadioButton 
            android:checked="true"
            android:id="@+id/boy"
            android:text="男"
            />
        <RadioButton 
            android:id="@+id/girl"
            android:text="女"
            />
    </RadioGroup>

現時点では簡単な表示設定のみを行っていますが、これらのコンポーネントをより意味のあるものにしたい場合は、今後学習するイベント処理と組み合わせる必要があります。

3. まとめ

(1) RadioGroup クラスをマスターすることは、ラジオ ボタンのコンテナを定義することと同じです。

(2) RadioButton クラスは、ラジオ ボタンの内容を定義するために使用されます。

Supongo que te gusta

Origin blog.csdn.net/weixin_41830242/article/details/131200767
Recomendado
Clasificación