简体   繁体   English

Android:如何以编程方式删除 XML 文件的所有元素

[英]Android: How to delete all the Elements of a XML-File programmatically

as the title states, how to delete all the elements of any given XML-File.如标题所述,如何删除任何给定 XML 文件的所有元素。 Let's say we have the following XML-File: example.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>

Question: How to delete two Button Elements from the example.xml - programmtically?问题:如何从示例中删除两个按钮元素。xml - 以编程方式?

public static void removeDirectChildren( Node parent )
{
  NodeList childNodes = parent.getChildNodes();
  while ( childNodes.getLength() > 0 )
  {
    parent.removeChild( childNodes.item( 0 ) );
  }
}

and you call it with the root element.你用根元素调用它。

Call removeAllViews() on the ConstraintLayout .ConstraintLayout上调用removeAllViews()

Call this method to remove all child views from the ViewGroup.调用此方法从 ViewGroup 中删除所有子视图。

contraintLayout.removeAllViews()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM