所以我有一段代码,在其中我在用户单击按钮时创建了一个 textView。因为它是在运行时之后创建的,所以我不确定 ID 是什么。但我想要做的是可以选择删除最后添加的 textView 并清除所有添加的 TextView。谢谢
private TextView createNewTextView(String text)
{
ArraySize++;
final LinearLayout mLayout=findViewById(R.id.linearLayout);
String newLine=System.getProperty("line.separator");
final LinearLayout.LayoutParams lparams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ViewGroup.LayoutParams params=(RelativeLayout.LayoutParams) mLayout.getLayoutParams();
final TextView textView=new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New texT:: "+text+newLine);
listOfNames.add(text);
return textView;
}
最佳答案
您可以在此路径 res/values/ids.xml 中创建一个 XML 文件;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="view_1" type="id"/>
</resources>
然后在 Java 中你添加 id,像这样
textView.setId(R.id.view_1);
当你想删除它的时候
LinearLayout mLayout=findViewById(R.id.linearLayout);//to find in this specific view
TextView textView = (TextView)mLayout.findViewById(R.id.view_1);
mLayout.removeView(textView);
关于java - 如何删除动态添加的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52413721/