例如:
private void Form1_Load(object sender, EventArgs e){
radioButton1.Checked = true
}
设置其属性Checked为真时即为选中。运行如下:
//假定界面上已经拖动了一个groupBox1private void button1_Click(object sender, EventArgs e)
{
RadioButton rdb = new RadioButton()
rdb.Text = "radioButton1"
rdb.Location = new Point(10, 10)
groupBox1.Controls.Add(rdb)
}
由于有许多的RadioButton是动态的,不是固定的一些,所以需要在代码中,动态的添加到RadioGroup中,可以采用下面的程度代码来实现。一、添加RadioButton到RadioGroup中( 以下为程序代码)
1. RadioGroup group
2. for(int i=0i<10i++)
3. {
4. RadioButton tempButton = new RadioButton(this)
5. tempButton.setBackgroundResource(R.drawable.xxx) // 设置RadioButton的背景图片
6. tempButton.setButtonDrawable(R.drawable.xxx) // 设置按钮的样式
7. tempButton.setPadding(80, 0, 0, 0)// 设置文字距离按钮四周的距离
8. tempButton.setText("按钮 " + i)
9. group.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
10. }
二、为RadioGroup添加事件处理,可以得到当前选择的RadioButton ( 以下为程序代码)
1. group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2.
3. @Override
4. public void onCheckedChanged(RadioGroup group, int checkedId) {
5. // TODO Auto-generated method stub
6. RadioButton tempButton = (RadioButton)findViewById(checkedId)// 通过RadioGroup的findViewById方法,找到ID为checkedID的RadioButton
7. // 以下就可以对这个RadioButton进行处理了
8. }
9. })
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)