【问题标题】:Android: How to delete all the Elements of a XML-File programmaticallyAndroid:如何以编程方式删除 XML 文件的所有元素
【发布时间】:2020-10-24 10:50:37
【问题描述】:

如标题所述,如何删除任何给定 XML 文件的所有元素。 假设我们有以下 XML 文件:example.xml:

<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity"
    android:id="@+id/idExample">

<Button
        android:id="@+id/beispiel2"
        android:onClick="onSelectDas"
        android:text="Check"
/>

<Button
        android:id="@+id/beispiel2"
        android:onClick="onSelectDas"
        android:text="Check"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

问题:如何从 example.xml 中删除两个按钮元素 - 以编程方式?

【问题讨论】:

    标签: java android xml android-studio android-constraintlayout


    【解决方案1】:
    public static void removeDirectChildren( Node parent )
    {
      NodeList childNodes = parent.getChildNodes();
      while ( childNodes.getLength() > 0 )
      {
        parent.removeChild( childNodes.item( 0 ) );
      }
    }
    
    

    然后你用根元素调用它。

    【讨论】:

      【解决方案2】:

      ConstraintLayout上调用removeAllViews()

      调用此方法从 ViewGroup 中删除所有子视图。

      contraintLayout.removeAllViews()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-30
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2012-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多