我的问题是,一旦我的editText字段被创建,我不知道如何使用我的应用程序中的另一个按钮逐个删除它们。我不太了解android:id元素,因为它与我的java代码中的setID()有关。我想我的主要问题是,如果我不知道他们的id,我如何弄清楚如何用他们的id瞄准动态创建的editText视图。这是我的代码,非常感谢任何帮助:
爪哇:
package com.zdowning.decisionmaker;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.lang.*;
import static com.zdowning.decisionmaker.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer();
}
});
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Add_Line();
}
});
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setId(numberOfLines++);
et.setText("Enter Answer #" + numberOfLines++);
ll.addView(et);
numberOfLines++;
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void Remove_Line() {
}
public void getAnswer() {
String[] options = new String[numberOfLines];
EditText text = (EditText)findViewById(R.id.editText2);
options[0] = text.getText().toString();
text = (EditText)findViewById(R.id.editText3);
options[1] = text.getText().toString();
text = (EditText)findViewById(R.id.editText4);
options[2] = text.getText().toString();
int number = (int)(Math.random() * 3);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">
<LinearLayout
android:id="@+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Your Question Here"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:id="@+id/linearLayoutDecisions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #2" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #1" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/add_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="+"
android:textColor="@android:color/background_light"
android:textSize="18sp"/>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="@+id/remove_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="-"
android:textColor="@android:color/background_light"
android:textSize="18sp"
android:layout_margin="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="DECIDE!"
android:textColor="@android:color/background_light"
android:textSize="18sp"
android:layout_centerInParent="true" />
</RelativeLayout>
<TextView
android:id="@+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:textColor="@android:color/black"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
android:id
与 setID
之间没有任何关系。
检查this answer以了解更多相关信息。
无论如何,实现删除生成的行的一种方法可以基于LinearLayout的子索引,如下所示:
private LinearLayout mEditTextContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
...
}
public void addLine() {
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
mEditTextContainer.addView(et);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void removeLine() {
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}