delphi RadioGroup里怎么添加单选按钮啊
我这边不管选中哪个单选按钮,RadioGroup1.ItemIndex得到的都是-1,我是先建了个RadioGroup组件,又把Radio组件拖进RadioGroup里的...
我这边不管选中哪个单选按钮,RadioGroup1.ItemIndex得到的都是-1,我是先建了个RadioGroup组件,又把Radio组件拖进RadioGroup里的
展开
推荐于2018-04-08 · 知道合伙人软件行家
关注
展开全部
先中后,点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();
}
}
});
下面上代码,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();
}
}
});
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询