Android控件入门-单选多选控件

单选:

xml:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="世界上最好的编程语言是什么?"
    android:textSize="20sp" />

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/php"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PHP"
        android:textSize="18sp" />

    <RadioButton
        android:id="@+id/java"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="JAVA"
        android:textSize="18sp" />

    <RadioButton
        android:id="@+id/C"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C"
        android:textSize="18sp" />

    <RadioButton
        android:id="@+id/python"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Python"
        android:textSize="18sp" />
</RadioGroup>
<Button
    android:id="@+id/submit"
    android:layout_width="166dp"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:text="提交" />

java:

private RadioButton ra;
ra = findViewById(R.id.java);
Button btnSubmit = findViewById(R.id.submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (ra.isChecked()) {
            Toast.makeText(MainActivity.this, "选择正确", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "选择错误", Toast.LENGTH_SHORT).show();
        }
    }
});

多选:

xml:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="你会使用什么编程语言?"
    android:textSize="20sp" />

<CheckBox
    android:id="@+id/php1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PHP" />

<CheckBox
    android:id="@+id/java1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Java" />

<CheckBox
    android:id="@+id/python1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Python" />

<CheckBox
    android:id="@+id/javascript"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="JavaScript" />

<TextView
    android:id="@+id/show"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="16sp" />

java:

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener
private CheckBox cb1, cb2, cb3, cb4;
private TextView show;
show = findViewById(R.id.show);
cb1 = findViewById(R.id.php1);
cb2 = findViewById(R.id.java1);
cb3 = findViewById(R.id.python1);
cb4 = findViewById(R.id.javascript);

cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
cb4.setOnCheckedChangeListener(this);
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    String str = "你会";

    if (cb1.isChecked()){
        str += cb1.getText() + ",";
    }
    if (cb2.isChecked()){
        str += cb2.getText() + ",";
    }
    if (cb3.isChecked()){
        str += cb3.getText() + ",";
    }
    if (cb4.isChecked()){
        str += cb4.getText() ;
    }

    show.setText(str);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值