java - 如何删除动态添加的 TextView

标签 java android textview

所以我有一段代码,在其中我在用户单击按钮时创建了一个 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/

相关文章:

java - Mockito 没有返回我需要的东西

java - 在java中为行之间存在的字符串编写正则表达式

user-interface - 如何在android中添加文本稀松布

java - 有没有办法在 Android 上复制文件夹? (非文件)

android - 从首选项打开的文件选择器 Intent

android - 如何让 TextView 增加高度

java - 如何识别像 split 这样的单词

java - Jersey 获取 HTTP 405,路径映射未按预期处理

java - 在 netbeans 中从 GUI 表单之外的类调用方法

AndroidNetworking 在 Kotlin 中的 suspendCoroutine 中永远不会返回