文章目录
- 单选框
-
- xml文件
- Activity代码
- 运行结果
- 多选框
-
- xml文件
- Activity代码
- 运行结果
单选框
xml文件
<LinearLayout xmlns:android="http:///apk/res/android" xmlns:tools="http:///tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择性别" android:textSize="23dp" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/btnMan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true"/> <RadioButton android:id="@+id/btnWoman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> </RadioGroup> <Button android:id="@+id/btnpost" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交"/> </LinearLayout>
Activity代码
package com.example.androidtest.component; import .AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import com.example.androidtest.R; public class RadioActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio); RadioGroup radgroup = (RadioGroup) findViewById(.radioGroup); //第一种获得单选按钮值的方法 //为radioGroup设置一个监听器:setOnCheckedChanged() radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radbtn = (RadioButton) findViewById(checkedId); Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show(); } }); Button btnchange = (Button) findViewById(.btnpost); //为radioGroup设置一个监听器:setOnCheckedChanged() btnchange.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i < radgroup.getChildCount(); i++) { RadioButton rd = (RadioButton) radgroup.getChildAt(i); if (rd.isChecked()) { Toast.makeText(getApplicationContext(), "点击提交按钮,获取你选择的是:" + rd.getText(), Toast.LENGTH_LONG).show(); break; } } } }); } }
运行结果
xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:///apk/res/android" xmlns:tools="http:///tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".component.CheckBoxActivity"> <CheckBox android:id="@+id/eat" android:layout_width="90dp" android:layout_height="wrap_content" android:text="吃饭" /> <CheckBox android:id="@+id/sleep" android:layout_width="90dp" android:layout_height="wrap_content" android:text="睡觉" /> <CheckBox android:id="@+id/da_dou" android:layout_width="90dp" android:layout_height="wrap_content" android:text="打豆豆" /> <Button android:id="@+id/checkBoxButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout>
Activity代码
package com.example.androidtest.component; import .AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.Toast; import com.example.androidtest.R; public class CheckBoxActivity extends AppCompatActivity { private CheckBox eat, sleep, da_dou; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_check_box); eat = findViewById(.eat); sleep = findViewById(.sleep); da_dou = findViewById(.da_dou); button = findViewById(.checkBoxButton); //单独的监控 eat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (buttonView.isChecked())Toast.makeText(CheckBoxActivity.this,buttonView.getText()+"被选中",Toast.LENGTH_SHORT).show(); } }); //通过一个按钮集体显示 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String msg = (eat.isChecked() ? eat.getText().toString() : "") + (sleep.isChecked() ? sleep.getText().toString() : "") + (da_dou.isChecked() ? da_dou.getText().toString() : ""); Toast.makeText(CheckBoxActivity.this,msg,Toast.LENGTH_SHORT).show(); } }); } }
运行结果