package com.gaodu;import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;public class RadioAct extends Activity {
private TextView text; public RadioAct() { } @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layear = new LinearLayout(this);
layear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText("请问你是?");
RadioGroup rdGroup = new RadioGroup(this);
rdGroup.setOrientation(LinearLayout.VERTICAL);
RadioButton rb1 = new RadioButton(this); rb1.setText("男");
RadioButton rb2 = new RadioButton(this);
rb2.setText("女");
rdGroup.addView(rb1, 1, ViewGroup.LayoutParams.FILL_PARENT);
rdGroup.addView(rb2, 2, ViewGroup.LayoutParams.FILL_PARENT);
rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == 1) { text.setText("男");
} else { text.setText("女");
} }
});
layear.addView(text, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layear.addView(rdGroup, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(layear);
}}
奇怪的是界面显示出了一行请问你是?,,而RadioGroup却没显示出来!
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;public class RadioAct extends Activity {
private TextView text; public RadioAct() { } @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layear = new LinearLayout(this);
layear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText("请问你是?");
RadioGroup rdGroup = new RadioGroup(this);
rdGroup.setOrientation(LinearLayout.VERTICAL);
RadioButton rb1 = new RadioButton(this); rb1.setText("男");
RadioButton rb2 = new RadioButton(this);
rb2.setText("女");
rdGroup.addView(rb1, 1, ViewGroup.LayoutParams.FILL_PARENT);
rdGroup.addView(rb2, 2, ViewGroup.LayoutParams.FILL_PARENT);
rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == 1) { text.setText("男");
} else { text.setText("女");
} }
});
layear.addView(text, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layear.addView(rdGroup, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(layear);
}}
奇怪的是界面显示出了一行请问你是?,,而RadioGroup却没显示出来!
解决方案 »
- 求助解決android4.2 make sdk 遇到的 error 45 的問題!
- 自定义相机,定点对焦
- 求Android 高手指点
- android的控件嵌套层次以多少层为宜?
- HTTP POST方式调用WebService的问题。
- android2.0的程序移植到android1.5上图片变形
- Android ndk 调用第三方so出现 Cannot load library: reloc_library ...cannot locate问题
- 问个关于自定义SeekBar外观的问题,纠结了蛮久
- 请教大佬
- 急求关于service存活问题的答案
- AlertDialog背景色
- Android工程代码重用问题
LayoutParams.WRAP_CONTENT)); 试试
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.LayoutParams;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;public class RadioAct extends Activity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layear = new LinearLayout(this);
layear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText("请问你是?");
RadioGroup rdGroup = new RadioGroup(this);
rdGroup.setOrientation(LinearLayout.VERTICAL);
RadioButton rb1 = new RadioButton(this); rb1.setText("男");
RadioButton rb2 = new RadioButton(this);
rb2.setText("女");
rdGroup.addView(rb1, RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rdGroup.addView(rb2, RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == 1) { text.setText("男");
} else { text.setText("女");
} }
});
layear.addView(text, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layear.addView(rdGroup, new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
setContentView(layear);
}
}
像这样改一下,rdGroup.addView(rb1, 1, ViewGroup.LayoutParams.FILL_PARENT); 的问题,1不是id而是宽度。