radiogroup(RadioGroup总结 )
一、C++builder中的radiogroup中怎样得到选中条目的值
一、这是一个简单得不能再简单的问题。
1、RadioGroup的ItemIndex指示了当前被选中的行数,注意从0开始计数。
2、RadioGroup的Items中,是一行一行的Strings,里面没有值可言,就是字串,被选中了哪一行的字,ItemIndex就对应着哪一行(0开始计),就取对应行的字串就可以了。
3、ItemIndex的改变关联着RadioGroup的点击事件。直接代码强行改变ItemIndex可能也会同时触发RadioGroup的点击事件,会不会自个测试。
4、具体的代码如下:(请在界面上放一个RadioGroup1和一个Text1,双击RadioGroup1,自动产生RadioGroup1的点击触发函数后,加入下面两行代码中的一行,根据你的需要)
Edit1->Text=IntToStr(RadioGroup1->ItemIndex);
Edit1->Text=RadioGroup1->Items->Strings[RadioGroup1->ItemIndex];
——————————————————————————————————
5、顺便:吐个槽,C++Builder是辣么好用的工具,首先是厂商的运作不力,其次不是微软的亲儿子,再次核心用的Delphi,使得用户群是辣么的少。其实C++Builder快捷与强大加于一身本应是无敌的存在。但真理缺往往不被人所认同!所以C++Builder也就成了一个没有钱途的工具。只有少量的编程爱好者会热爱它!
6、不过,如果你爱上了C++Builder,也别灰心,计算机语言这东西,懂了一个,别的就可以触类旁通了,思想是一样的,不同的只是工具而已。
二、android的radiogroup为什么选择两个
项目中遇到多个RadioGroup中单选RadioButton,设置了默认选中第一个.然后就能选中两个RadioButton....
我开始这样给设置默认选中一个的:
for(int j= 0; j< newList.get(position).getList().size(); j++){
RadioButton radioButton= new RadioButton(context);
radioButton.setTextSize(9);
radioButton.setText(newList.get(position).getList().get(j)
.get("dishname").toString());
radioButton.setTag(newList.get(position).getList().get(j)
.get("dishid").toString());
radioGroup.addView(radioButton, j);
if(j==0){
radioButton.setCheck(true);
}
}
就是中给radioButton设置为选中...
网上查找了下类似的情况如这篇文章,都没有解决我的问题.
最后研究了下 android官方Api和部分 RadioGroup的源代码后发现.其实很简单
我们不需要设置RadioButton的默认选中,这样会使RadioButton一直处于选中状态.
我们应该给RadioGroup设置选中的RadioButton,也就是说
把 if(j==0){
radioButton.setCheck(true);
}
更改为
if(j==0){
radioGroup.check(radioButton.getId());
}
轻松搞定..哎呦了个去,官方Api和源码是个好东西啊.
三、如何获取RadioGroup中RadioButton的值
1,获取RadioGroup控件:
RadioGroup radioGroup=(RadioGroup)findViewById(R.id.myRadioGroup);
2,获取RadioButton控件;
RadioButton radioButton=(RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
3,获取选中的radio的值:
String text= radioButton.getText().toString();
4,为radioGroup添加监听事件,用来监听组件内部的事件响应:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId){
//在这个函数里面用来改变选择的radioButton的数值,以及与其值相关的//任何操作,详见下文
selectRadioBtn();
}
})
;
5,在onCreat中需要初始化上面的四条信息;
6,整体的使用样例:
布局文件xml中的内容:
<RadioGroup
android:id="@+id/sex_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男"/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
代码实现:
private RadioGroup mSex_group;
private RadioButton mMale;
private RadioButton mFemale;
private String sexName;
mSex_group=(RadioGroup) findViewById(R.id.sex_group);
mMale=(RadioButton) findViewById(R.id.male);
mFemale=(RadioButton) findViewById(R.id.female);
mSex_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId){
if(mMale.getId()== checkedId){
sexName= mMale.getText().toString();
} else if(mFemale.getId()== checkedId){
sexName= mFemale.getText().toString();
}
}
});