使用者可在一个RadioGroup底下,建立多个RadioButton。
而RadioGroup与CheckBox不同的是,在同个RadioGroup底下,使用者只能勾选一个选项。
RadioGroup适合用於问卷调查。
以下是本次RadioGroup范例xml的部分
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用餐体验"
android:textSize="25sp"/>
<RadioGroup android:id="@+id/mainmeal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton android:id="@+id/good"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="非常满意"
android:textSize="15sp" />
<RadioButton android:id="@+id/soso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通"
android:textSize="15sp"/>
<RadioButton android:id="@+id/bad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不满意"
android:textSize="15sp"/>
</RadioGroup>
<TextView android:id="@+id/feel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
范例以用餐体验调查为例,在RadioGroup当中建立三个RadioButton分别是非常满意、普通以及不满意。
值得注意的是RarioGroup的orientation="horizontal,因此RadioButton会横向排列。
再来是java的部分
package com.example.radiogroup;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView feel;
private RadioGroup mainmeal;
private RadioButton good,soso,bad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feel = findViewById(R.id.feel);
mainmeal = findViewById(R.id.mainmeal);
good = findViewById(R.id.good);
soso = findViewById(R.id.soso);
bad = findViewById(R.id.bad);
mainmeal.setOnCheckedChangeListener(mainmealListener);
}
private RadioGroup.OnCheckedChangeListener mainmealListener=new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.good)
feel.setText("您的支持是我们向前的动力");
if (checkedId == R.id.soso)
feel.setText("我们会继续加油!");
if (checkedId == R.id.bad)
feel.setText("抱歉让您失望了");
}
};
}
mainmeal.setOnCheckedChangeListener(mainmealListener);为设定RadioGroup核选後触发事件的listener
其下方为listener程序内容,
分别在核选三个选项时显示不同的文字於TextView,
如下方所示
GitHub Repo https://github.com/b2etw/Spring-Kotlin...
Q: 别人网站上看到的动态效果变化很多,还是引写好的套件(库)进专案吧? A: 只引进一个套件(库...
越接近完赛越害怕自己今天到底发文了没!这几天早中晚都会反覆确认有没有发文,毕竟坚持30天写技术文章那...
目标做成: 结果作成: code: html: <!DOCTYPE html> <...
今天要把「关於」的部分写完 首先因为架构是差不多的,所以我打算使用json做成文档然後再抓取渲染 先...