radiogroup,delphi RadioGroup里怎么添加单选按钮啊
尊敬的读者,radiogroup和delphi RadioGroup里怎么添加单选按钮啊是当前备受关注的话题,但许多人对其仍存在疑惑。在本篇文章中,我将为你提供清晰的解释和深入的分析,希望能满足你的求知欲望。
delphi RadioGroup里怎么添加单选按钮啊
先中后,点RadioGroup测试按钮,可在标题栏显示选择结果,点清除可以清除选择。
下面上代码,main.xml:
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/b1"
android:id="@+id/RG">
<!--默认选中b1-->
<RadioButton
android:text="1"
android:id="@+id/b1"
/>
<RadioButton
android:text="2"
android:id="@+id/b2"
/>
<RadioButton
android:text="3"
android:id="@+id/b3"
/>
</RadioGroup>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/show"
android:text="RadioGroup测试"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/clear"
android:text="清除"
/>
程序代码:
package com.pocketdigi;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class main extends Activity{
/** Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTitle("RadioGroup测试");
setContentView(R.layout.main);
RGDemo();
}
RadioGroup rg;
RadioButton b1;
RadioButton b2;
RadioButton b3;
public void RGDemo(){
rg=(RadioGroup)findViewById(R.id.RG);
b1=(RadioButton)findViewById(R.id.b1);
b2=(RadioButton)findViewById(R.id.b2);
b3=(RadioButton)findViewById(R.id.b3);
Button clr=(Button)findViewById(R.id.clear);
clr.setOnClickListener(clear);
Button echo=(Button)findViewById(R.id.show);
echo.setOnClickListener(show);
}
private Button.OnClickListener clear=new OnClickListener(){
@Override
public void onClick(View v){
// TODO Auto-generated method stub
rg.clearCheck();
setTitle("RadioGroup测试");
}
};
private OnClickListener show=new OnClickListener(){
@Override
public void onClick(View v){
// TODO Auto-generated method stub
if(b1.isChecked()){
setTitle("1");
}
if(b2.isChecked()){
setTitle("2");
}
if(b3.isChecked()){
setTitle("3");
}
}
};
}
RadioGroup有一个onCheckChangeListener监听器,可以通过监听器的onCheckedChanged方法捕捉到点击事件,onCheckedChanged方法会传入一个int型的checkedId,可以通过对比传入的checkedId和RadioButton的ID,来确定被点中的选项.
rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId){
// TODO Auto-generated method stub
if(checkedId==b1.getId()){
Toast.makeText(main.this,"b1选中", Toast.LENGTH_LONG).show();
}
if(checkedId==b2.getId()){
Toast.makeText(main.this,"b2选中", Toast.LENGTH_LONG).show();
}
if(checkedId==b3.getId()){
Toast.makeText(main.this,"b3选中", Toast.LENGTH_LONG).show();
}
}
});
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的clearCheck()使用方法
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听
01 RadioGroup相关属性:
02
03 RadioGroup.getCheckedRadioButtonId();--获取选中按钮的id
04
05 RadioGroup.clearCheck();//---清除选中状态
06
07 RadioGroup.check(int id);//---通过参入选项id来设置该选项为选中状态如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作
08
09 setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener);//--一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数
10
11 addView(View child, int index, ViewGroup.LayoutParams params);//---使用指定的布局参数添加一个子视图
12
13//参数 child所要添加的子视图 index将要添加子视图的位置 params所要添加的子视图的布局参数
14
15 RadioButton.getText();//获取单选框的值
16
17//此外,RadioButton的checked属性设置为true,代码里调用RadioButton的check(id)方法,不会触发onCheckedChanged事件
RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置
看案例:
1.定义布局文件:
01<?xml version="1.0" encoding="utf-8"?>
02<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
03 android:layout_width="match_parent"
04 android:layout_height="match_parent">
05<LinearLayout
06 android:orientation="vertical"
07 android:layout_width="match_parent"
08 android:layout_height="wrap_content"
09 android:layout_marginRight="5dp">
10
11<TextView
12 android:id="@+id/radiogroup_info_id"
13 android:layout_width="228px"
14 android:layout_height="wrap_content"
15 android:text="我选择的是...?"
16 android:textSize="30sp"
17/>
18
19<RadioGroup
20 android:id="@+id/radioGroup_sex_id"
21 android:layout_width="match_parent"
22 android:layout_height="match_parent"
23>
24<RadioButton
25 android:id="@+id/boy_id"
26 android:layout_width="match_parent"
27 android:layout_height="match_parent"
28 android:text="Boy"
29/>
30<RadioButton
31 android:id="@+id/girl_id"
32 android:layout_width="match_parent"
33 android:layout_height="match_parent"
34 android:text="Girl"
35/>
36
37</RadioGroup>
38<Button
39 android:id="@+id/radio_clear"
40 android:layout_width="match_parent"
41 android:layout_height="match_parent"
42 android:text="清除选中按钮"
43/>
44
45<Button
46 android:id="@+id/radio_add_child"
47 android:layout_width="match_parent"
48 android:layout_height="match_parent"
49 android:text="添加单选项"
50/>
51
52</LinearLayout>
53</ScrollView>
2.java代码文件
01 package com.dream.app.start.first.radiobutton;
02
03 import com.dream.app.start.R;
04 import com.dream.app.start.R.id;
05 import com.dream.app.start.R.layout;
06 import com.dream.app.start.three.utils.PublicClass;
07
08 import android.app.Activity;
09 import android.os.Bundle;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.view.ViewGroup.LayoutParams;
13 import android.widget.Button;
14 import android.widget.RadioButton;
15 import android.widget.RadioGroup;
16 import android.widget.RadioGroup.OnCheckedChangeListener;
17 import android.widget.TextView;
18 import android.widget.ToggleButton;
19
20 public class RadioButtonDemo extends PublicClass{
21 private TextView textView=null;
22 private RadioGroup radioGroup=null;
23 private RadioButton radioButton_boy,radioButton_girl;
24 private Button radio_clear,child;
25/*(non-Javadoc)
26*<a rel="external nofollow" href="http://my.oschina.net/u/244147" target="_blank" rel="nofollow">@see</a> android.app.Activity#onCreate(android.os.Bundle)
27*/
28@Override
29 protected void onCreate(Bundle savedInstanceState){
30// TODO Auto-generated method stub
31 super.onCreate(savedInstanceState);
32
33 setContentView(R.layout.layout_frist_radiobuton);
34
35 textView=(TextView)findViewById(R.id.radiogroup_info_id);
36
37//radioGroup
38 radioGroup=(RadioGroup)findViewById(R.id.radioGroup_sex_id);
39 radioButton_boy=(RadioButton)findViewById(R.id.boy_id);
40 radioButton_girl=(RadioButton)findViewById(R.id.girl_id);
41 child=(Button)findViewById(R.id.radio_add_child);
42//---
43 radioGroup.setOnCheckedChangeListener(listen);
44 radio_clear=(Button)findViewById(R.id.radio_clear);
45 radio_clear.setOnClickListener(onClick);
46 child.setOnClickListener(onClick);
47}
48
49 private OnCheckedChangeListener listen=new OnCheckedChangeListener(){
50
51@Override
52 public void onCheckedChanged(RadioGroup group, int checkedId){
53 int id= group.getCheckedRadioButtonId();
54 switch(group.getCheckedRadioButtonId()){
55 case R.id.girl_id:
56 textView.setText("我选择的是:"+radioButton_girl.getText());
57 break;
58 case R.id.boy_id:
59 textView.setText("我选择的是:"+radioButton_boy.getText());
60 break;
61 default:
62 textView.setText("我选择的是:新增");
63 break;
64}
65
66}
67};
68 private OnClickListener onClick=new OnClickListener(){
69
70@Override
71 public void onClick(View v){
72 radio_clear=(Button)v;
73 switch(radio_clear.getId()){
74 case R.id.radio_clear:
75 radioGroup.check(-1);//清除选项
76// radioGroup.clearCheck();//清除选项
77 textView.setText("我选择的是...?");
78 break;
79 case R.id.radio_add_child:
80//新增子
81 RadioButton newRadio=new RadioButton(getApplicationContext());
82 newRadio.setText("新增");
83 radioGroup.addView(newRadio, radioGroup.getChildCount());
84 break;
85//
86
87 default:
88 break;
89}
90}
91};
如果你还想了解更多这方面的信息,记得收藏关注本站。