Android studio APP开发 单选框和复选框 |
您所在的位置:网站首页 › 所选项目的复选框怎么设置出来 › Android studio APP开发 单选框和复选框 |
单选框和复选框
单选按钮和复选按钮都是普通按钮Button的子类,所以可以使用所有Button的方法和属性。也有自己特有的属性方法 单选框单选框就是在多个选项中只选择一个。 在Android中,单选按钮用RadioButton表示,而RadioButton类又是Button子类。 通常情况下,RadioButton组件需要与RadioGroup组件一起使用。 设置单选框
通常在以下两种情况下获取单选框组中选中项的值。 在改变单选框组的值时获取在单击其他按钮时获取获取单选框组选值的基本步骤如下: 找到这个单选框组。通过RadioGroup的id调用setOnCheckedChangeListener方法,根据checkedId来获取被选中的单选按钮。通过getText来获取单选按钮的值进行其他操作 在改变单选框组的值时获取为了能够清晰展示单选框选择的效果,添加了一个TextView来实时显示单选框获取的值。 修改后的布局管理器如下:
要获取单选按钮组中按钮的值,首先要做的就是在被点击的其他按钮的监听事件onClick中获取单选按钮组中选中按钮的id,再获得其值。获得id的过程可以通过for循环遍历所有单选框,根据isChecked()的方法判断按钮是否被选中,当被选中时,通过getText()来获取值。 代码实现如下: public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //在改变单选框组的值时获取 final RadioGroup sex = (RadioGroup) findViewById(R.id.radioGroup1); sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton r = (RadioButton) findViewById(checkedId); String str = r.getText().toString(); TextView textView = (TextView) findViewById(R.id.textshow); textView.setText(str); } }); //单击其他按钮时获取 Button button = (Button) findViewById(R.id.button01); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str1; for(int i=0;i str1 = r.getText().toString(); TextView textView1 = (TextView) findViewById(R.id.textshow1); textView1.setText(str1); break; } } } }); } }
复选框可以进行多项选择,每一个复选框都提供了选中和补选中两种状态。在Android 中,复选框用CheckBox()方法来表示,CheckBox()的子类是,所以可以直接使用Button来实现。 设置复选框在之前的布局管理器中添加如下代码: 在Activity中添加如下两种方法。并且在onCreate中调用。 为了不让Activity中的代码看起来很乱,所以写在一个方法里。 public void checkBox_button(){ final CheckBox hobby01 = (CheckBox) findViewById(R.id.hobby1); final CheckBox hobby02 = (CheckBox) findViewById(R.id.hobby2); final CheckBox hobby03 = (CheckBox) findViewById(R.id.hobby3); final CheckBox hobby04 = (CheckBox) findViewById(R.id.hobby4); Button button = (Button) findViewById(R.id.button02); hobby01.setOnCheckedChangeListener(checkBox_listener); hobby02.setOnCheckedChangeListener(checkBox_listener); hobby03.setOnCheckedChangeListener(checkBox_listener); hobby04.setOnCheckedChangeListener(checkBox_listener); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String hob = ""; if(hobby01.isChecked()){ hob += hobby01.getText().toString() + " "; } if(hobby02.isChecked()){ hob += hobby02.getText().toString() + " "; } if(hobby03.isChecked()){ hob += hobby03.getText().toString() + " "; } if(hobby04.isChecked()){ hob += hobby04.getText().toString() + " "; } Toast.makeText(SecondActivity.this,hob,Toast.LENGTH_SHORT).show(); } }); } private CompoundButton.OnCheckedChangeListener checkBox_listener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ Toast.makeText(SecondActivity.this,buttonView.getText().toString(),Toast.LENGTH_SHORT).show(); } } };
|
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |