我正在尝试添加一个“添加”按钮,以便根据按钮左侧的编辑文本向布局中添加另一个按钮。目的是让人们列出他们房子里的房间,然后在输入每个房间时生成一个新的按钮,这样他们就可以单击该房间,然后开始工作在下一页上。
我已经完成了一个XML布局,然后我意识到我正在“以编程方式”添加按钮,所以我重新以编程方式制作了布局,然后在switch/case(这是我做onclicks的方式)中尝试添加按钮到视图中,但这变得非常棘手。我想在编辑文本和添加按钮下面有一个滚动视图,并且随着他们将所有房间添加到他们的房子中,它最终会填充一个可滚动的按钮列表,用于他们整个家庭。是否有一种方法可以以编程方式将按钮添加到XML布局中。我想你可以,但是我尝试的所有内容都不起作用。
感谢大家的帮助,非常感谢您提供的任何建议。
第一次编辑(响应Tanuj的解决方案)
我的XML文件(不确定我们是否要使用此文件还是仅使用Java):
我已经完成了一个XML布局,然后我意识到我正在“以编程方式”添加按钮,所以我重新以编程方式制作了布局,然后在switch/case(这是我做onclicks的方式)中尝试添加按钮到视图中,但这变得非常棘手。我想在编辑文本和添加按钮下面有一个滚动视图,并且随着他们将所有房间添加到他们的房子中,它最终会填充一个可滚动的按钮列表,用于他们整个家庭。是否有一种方法可以以编程方式将按钮添加到XML布局中。我想你可以,但是我尝试的所有内容都不起作用。
感谢大家的帮助,非常感谢您提供的任何建议。
第一次编辑(响应Tanuj的解决方案)
我的XML文件(不确定我们是否要使用此文件还是仅使用Java):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvAddARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvAddARoom" />
<EditText
android:id="@+id/etAddARoom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/etAddARoom" />
<Button
android:id="@+id/btnAddARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnAdd" />
<TextView
android:id="@+id/tvSelectARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvSelectARoom" />
<TextView
android:id="@+id/tvNoRooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvNoRooms" />
<Button
android:id="@+id/btnViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnViewAll" />
</LinearLayout>
关于 Java。这并不完全正确,因为在我创建的 Java 中,我正在创建整个布局而不是使用上面的布局。我不确定是否可以将两者联系起来。
package com.bluej.movingbuddy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
//import android.widget.ScrollView;
import android.widget.TextView;
public class EstimatorByRoom extends Activity implements OnClickListener {
String roomName;
EditText etAddARoom;
LinearLayout layout;
LinearLayout.LayoutParams layoutParam;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.estimatorbyroom);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
//create a layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//create a text view
TextView tvAddARoom = new TextView(this);
tvAddARoom.setText("Add a Room");
tvAddARoom.setLayoutParams(params);
//create an edittext
EditText etAddARoom = new EditText(this);
etAddARoom.setHint("Living Room, Dining Room, etc.");
etAddARoom.setLayoutParams(params);
//create a button
Button btnAddARoom = new Button(this);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
//adds the textview
layout.addView(tvAddARoom);
//add the edittext
layout.addView(etAddARoom);
//add the button
layout.addView(btnAddARoom);
//create the layout param for the layout
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
this.addContentView(layout, layoutParam);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnAddARoom:
//add a room
//this part isn't working!
roomName = etAddARoom.getText().toString();
Button createdButton = new Button(this);
createdButton.setText(roomName);
layout.addView(createdButton);
this.addContentView(layout, layoutParam);
//if no rooms make tvnorooms disappear
break;
}
}
}
this.addContentView(layout, layoutParam);
。 - Tanuj Wadhwa