版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MR_HJY/article/details/87888230
单选:
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); }