Android 如何通过单击按钮添加和删除线性布局
我尝试使用两个按钮来动态添加和删除线性布局Android 如何通过单击按钮添加和删除线性布局,android,android-linearlayout,Android,Android Linearlayout,我尝试使用两个按钮来动态添加和删除线性布局 protected void createT () { // ----------------------------------------------- count++; LinearLayout temp_ll, frame; frame = new LinearLayout(this); frame.setOrientation(LinearLayout.VERTICAL); frame.
protected void createT () {
// -----------------------------------------------
count++;
LinearLayout temp_ll, frame;
frame = new LinearLayout(this);
frame.setOrientation(LinearLayout.VERTICAL);
frame.setId(count);
EditText temp1, temp2;
for (int i=0; i<numClass; i++) {
temp_ll = new LinearLayout(this);
temp_ll.setOrientation(LinearLayout.HORIZONTAL);
temp1 = new EditText(this);
temp2 = new EditText(this);
temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
temp1.setHint("class name");
temp2.setHint("grade");
temp_ll.addView(temp1);
temp_ll.addView(temp2);
frame.addView(temp_ll);
}
ll.addView(frame);
}
protected void deleteT() {
// --------------------------------------
if (count > 0) {
LinearLayout temp = new LinearLyout(this);
temp = (LinearLayout) findViewById(count);
temp.removeAllViews();
temp.setVisibility(View.GONE);
count--;
}
}
受保护的void createT(){
// -----------------------------------------------
计数++;
线性布局温度,框架;
框架=新的线性布局(本);
框架。设置方向(线性布局。垂直);
frame.setId(计数);
编辑文本temp1、temp2;
对于(int i=0;i 0){
LinearLayout temp=新的LinearLayout(此);
temp=(线性布局)findViewById(计数);
临时删除所有视图();
温度设置可见性(视图消失);
计数--;
}
}
- 每次我按下“添加”按钮时,它都会调用
createT()
- 每次我按下del按钮,它都会调用
deleteT()
linearLayout
id。第一次按add按钮x次,然后按del按钮,一切正常
问题是,在第二次按添加后。我检查了代码,发现deleteT()方法中有一个小错误,您删除了LinearLayout中的所有视图,但没有从根布局中删除实际布局 另一个建议是,当您想要使用findviewbyid方法时,不需要新的LinearLayout实例,而需要使用ll.removeView(View)方法来删除动态LinearLayout容器 代码如下:
private int count;
private LinearLayout ll;
private final int numClass = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll = (LinearLayout) findViewById(R.id.main_linearlayout);
}
public void createT(View view) {
count++;
final LinearLayout frame = new LinearLayout(this);
frame.setOrientation(LinearLayout.VERTICAL);
frame.setId(count);
for (int i = 0; i < numClass; i++) {
final LinearLayout temp_ll = new LinearLayout(this);
final EditText temp1;
final EditText temp2;
temp_ll.setOrientation(LinearLayout.HORIZONTAL);
temp1 = new EditText(this);
temp2 = new EditText(this);
temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
temp1.setHint("class name");
temp2.setHint("grade");
temp_ll.addView(temp1);
temp_ll.addView(temp2);
frame.addView(temp_ll);
}
ll.addView(frame);
}
public void deleteT(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) ll.findViewById(count);
temp.removeAllViews();
ll.removeView(temp);
count--;
}
}
私有整数计数;
私人线路布局;
私有最终整数类=1;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll=(线性布局)findViewById(R.id.main\U线性布局);
}
公共void createT(视图){
计数++;
最终线性布局框架=新的线性布局(本);
框架。设置方向(线性布局。垂直);
frame.setId(计数);
for(int i=0;i0){
最终线性布局温度=(线性布局)ll.findViewById(计数);
临时删除所有视图();
ll.移除视图(临时);
计数--;
}
}
以及布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/main_linearlayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:onClick="createT" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete"
android:onClick="deleteT" />
</LinearLayout>
我希望有帮助 我检查了您的代码,发现deleteT()方法中有一个小错误,您删除了LinearLayout中的所有视图,但没有从根布局中删除实际布局 另一个建议是,当您想要使用findviewbyid方法时,不需要新的LinearLayout实例,而需要使用ll.removeView(View)方法来删除动态LinearLayout容器 代码如下:
private int count;
private LinearLayout ll;
private final int numClass = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll = (LinearLayout) findViewById(R.id.main_linearlayout);
}
public void createT(View view) {
count++;
final LinearLayout frame = new LinearLayout(this);
frame.setOrientation(LinearLayout.VERTICAL);
frame.setId(count);
for (int i = 0; i < numClass; i++) {
final LinearLayout temp_ll = new LinearLayout(this);
final EditText temp1;
final EditText temp2;
temp_ll.setOrientation(LinearLayout.HORIZONTAL);
temp1 = new EditText(this);
temp2 = new EditText(this);
temp2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
temp1.setHint("class name");
temp2.setHint("grade");
temp_ll.addView(temp1);
temp_ll.addView(temp2);
frame.addView(temp_ll);
}
ll.addView(frame);
}
public void deleteT(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) ll.findViewById(count);
temp.removeAllViews();
ll.removeView(temp);
count--;
}
}
私有整数计数;
私人线路布局;
私有最终整数类=1;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll=(线性布局)findViewById(R.id.main\U线性布局);
}
公共void createT(视图){
计数++;
最终线性布局框架=新的线性布局(本);
框架。设置方向(线性布局。垂直);
frame.setId(计数);
for(int i=0;i0){
最终线性布局温度=(线性布局)ll.findViewById(计数);
临时删除所有视图();
ll.移除视图(临时);
计数--;
}
}
以及布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/main_linearlayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:onClick="createT" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete"
android:onClick="deleteT" />
</LinearLayout>
我希望有帮助 没有错误,只是“删除”按钮在第一次删除LinearLayout或任何特定布局后没有响应。。。上下阵型?我能知道你为什么要这么做吗?如果有一种简单的方法可以实现你的目标,那么有人可能会帮助你。方法是按下“添加”按钮,创建线性布局,以便稍后添加一些文本视图、编辑文本。和“del”按钮删除特定的线性布局(以及其中的所有对象)。“计数”用于将ID设置为linearLayout,也用于查找ID以删除此项用于Dheresh Singh。。。。从down到upno error,仅“del”按钮在第一次要删除LinearLayout或任何特定布局后不响应。。。上下阵型?我能知道你为什么要这么做吗?如果有一种简单的方法可以实现你的目标,那么有人可能会帮助你。方法是按下“添加”按钮,创建线性布局,以便稍后添加一些文本视图、编辑文本。和“del”按钮删除特定的线性布局(以及其中的所有对象)。“计数”用于将ID设置为linearLayout,也用于查找ID以删除此项用于Dheresh Singh。。。。从下到上如果您检查我的代码上面有一行:{temp.setVisibility(View.GONE);}--这将删除根布局。我哈