【发布时间】:2011-04-05 04:02:03
【问题描述】:
我想在运行时在我的 android 应用程序中动态创建一组复选框。当应用程序运行时,除了按钮之外什么都没有显示。我忘记了什么?提前致谢!
public class DataNotificationSurvey extends Activity {
private Date timeStamp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datanotifylayout);
final Button notifySubmitButton = (Button) findViewById(R.id.notifySubmitButton);
TableLayout t1 = (TableLayout)findViewById(R.id.notificationTableLayout);
for(int i = 0; i < 5; i++) {
TableRow tr = new TableRow(this);
CheckBox chk = new CheckBox(this);
chk.setText(Integer.toString(i));
tr.addView(chk);
t1.addView(tr);
}
notifySubmitButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
//My xml layout, will be changing this later to the one posted below to see if it works.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/notificationLinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/notificationTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Static Button"/>
</TableRow>
</TableLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/notifySubmitButton"
android:text="Submit"></Button>
</LinearLayout>
【问题讨论】:
-
请同时发布您的layout.xml...代码看起来没问题,所以问题可能就在那里。
-
@harpo:添加了 xml,我稍后会更改以反映作为答案发布的 xml。如果它有效,我会告诉你。但是,如果您在 xml 或其他任何地方发现问题,请告诉我。谢谢
标签: java android dynamic checkbox