亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何刪除動態添加的 TextView

如何刪除動態添加的 TextView

慕碼人8056858 2021-09-29 13:45:12
所以我有一段代碼,我在用戶點擊按鈕時創建了一個 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;    }
查看完整描述

3 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

您removeViewAt與索引一起使用


final LinearLayout mLayout=findViewById(R.id.linearLayout)

mLayout.removeViewAt(mLayout.getChildCount()-1); // get the last view


//or using index counter variable

//mLayout.removeViewAt(ArraySize-1); // adjust the value accordingly

或者您可以使用removeView獲取視圖并刪除它


final LinearLayout mLayout=findViewById(R.id.linearLayout)

View v = mLayout.getChildAt(mLayout.getChildCount()-1);

mLayout.removeView(v);


查看完整回答
反對 回復 2021-09-29
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

您可以在此路徑 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);


查看完整回答
反對 回復 2021-09-29
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

正如在這個問題中提到的,你可以簡單地使用

((ViewGroup) textView.getParent()).removeView(textView);


查看完整回答
反對 回復 2021-09-29
  • 3 回答
  • 0 關注
  • 470 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號