【问题标题】:Remove layout on a button click programmatically in android studio在android studio中以编程方式删除按钮单击的布局
【发布时间】:2022-08-18 19:57:39
【问题描述】:

我在单击按钮时添加布​​局。

 private void addLayout() {
    layout2 = LayoutInflater.from(mContext).inflate(R.layout.product_layout, mLinearLayout, false);
    productAuto = (AutoCompleteTextView) layout2.findViewById(R.id.tv_product);
    qtyEditText = (EditText) layout2.findViewById(R.id.prod_qty);
    prodPriceEditText = (EditText)layout2.findViewById(R.id.prod_price);
    prodSpecsEditText = (EditText)layout2.findViewById(R.id.prod_specs);
    removeProduct = (Button)layout2.findViewById(R.id.btn_rmv);
    mLinearLayout.addView(layout2);
    setProd();
    this.initListenersPrd(getActivity());
}
private void initListenersPrd(final Context context) {
    productAuto.setOnItemClickListener((parent, view, position, id) -> {
        String newName = parent.getItemAtPosition(position).toString();
        ProductDetail productDetail = mProductManager.getProductByPrdName(newName);
        if (productDetail != null) {
           this.prodPriceEditText.setText(productDetail.getPrice());
           this.prodSpecsEditText.setText(productDetail.getProductSpec());
        }
    });

    removeProduct.setOnClickListener(v -> {
       if (mLinearLayout.getChildCount()>0)
       {
           ((LinearLayout)mLinearLayout.getParent()).removeView(mLinearLayout);
       }

    });}

现在我想从应用程序中删除一个特定的产品表单,所以我在单击删除按钮时完成了((LinearLayout)mLinearLayout.getParent()).removeView(mLinearLayout);。但它会删除动态添加的整个布局。

图形用户界面

正如您在上图中看到的,我添加了 4-5 个产品布局,但是当我单击删除按钮时,我删除了所有布局。

更新 1

我在主布局中添加了以下布局,然后以编程方式在其中调用产品布局

 <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
            android:id=\"@+id/ll_prod\"
            android:layout_width=\"fill_parent\"
            android:layout_height=\"fill_parent\"
            android:orientation=\"vertical\" >

        </LinearLayout>

产品布局是单独创建的,我在我的主布局中调用它。

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!-- Copyright (C) 2014 The Android Open Source Project

 Licensed under the Apache License, Version 2.0 (the \"License\");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an \"AS IS\" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -->
 <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
          android:layout_width=\"match_parent\"
          android:layout_height=\"match_parent\"
          android:orientation=\"vertical\">
<LinearLayout
    android:id=\"@+id/ll_out\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"wrap_content\"
    android:background=\"@drawable/background_round\"
    android:orientation=\"vertical\"
    android:padding=\"5sp\">


    <LinearLayout
        android:layout_width=\"match_parent\"
        android:layout_height=\"match_parent\"
        android:layout_marginTop=\"10sp\"
        android:orientation=\"horizontal\">

        <AutoCompleteTextView
            android:id=\"@+id/tv_product\"
            android:layout_width=\"match_parent\"
            android:layout_height=\"wrap_content\"
            android:layout_gravity=\"left|center_vertical\"
            android:gravity=\"left\"
            android:hint=\"Enter Product\"
            android:inputType=\"text\" />
    </LinearLayout>

    <LinearLayout
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        android:layout_marginTop=\"10sp\"
        android:orientation=\"horizontal\">
        <LinearLayout
            android:layout_width=\"0dp\"
            android:layout_height=\"wrap_content\"
            android:layout_weight=\".5\"
            android:orientation=\"vertical\">

            <EditText
                android:id=\"@+id/prod_qty\"
                android:layout_width=\"match_parent\"
                android:layout_height=\"wrap_content\"
                android:editable=\"false\"
                android:focusable=\"true\"
                android:focusableInTouchMode=\"true\"
                android:hint=\"Enter Quantity\"
                android:gravity=\"left\"
                android:inputType=\"number\" />
        </LinearLayout>
        <LinearLayout
            android:layout_width=\"0dp\"
            android:layout_height=\"wrap_content\"
            android:layout_weight=\".5\"
            android:orientation=\"vertical\">
            <EditText
                android:id=\"@+id/prod_price\"
                android:layout_width=\"match_parent\"
                android:layout_height=\"wrap_content\"
                android:editable=\"false\"
                android:focusable=\"false\"
                android:focusableInTouchMode=\"false\"
                android:hint=\"Prod Price\"
                android:gravity=\"left\"
                android:inputType=\"none\" />
        </LinearLayout>

        <LinearLayout
            android:layout_width=\"0dp\"
            android:layout_height=\"wrap_content\"
            android:layout_weight=\".5\"
            android:orientation=\"vertical\">

            <EditText
                android:id=\"@+id/prod_specs\"
                android:layout_width=\"match_parent\"
                android:layout_height=\"wrap_content\"
                android:editable=\"false\"
                android:focusable=\"false\"
                android:focusableInTouchMode=\"false\"
                android:gravity=\"left\"
                android:hint=\"Prod Specs\"
                android:inputType=\"none\" />

        </LinearLayout>


    </LinearLayout>
    <LinearLayout
        android:layout_width=\"match_parent\"
        android:layout_height=\"wrap_content\"
        android:layout_marginBottom=\"1dp\"
        android:layout_marginTop=\"1dp\"
        android:padding=\"0dp\">

        <Button
            android:id=\"@+id/btn_rmv\"
            android:layout_width=\"match_parent\"
            android:layout_height=\"wrap_content\"
            android:text=\"Remove Product\"
            android:textColor=\"@color/white\" />
    </LinearLayout>
</LinearLayout>

每当我单击Add New Product Button 时,都会调用上述布局。

如何删除特定布局?

任何帮助将不胜感激。

    标签: android android-linearlayout android-view


    【解决方案1】:

    您在视图层次结构中修剪得太高。如果按钮是线性布局要删除,您可以在片段中执行以下操作。 (对于一个类似活动.)

    将五个子布局添加到顶级布局。

    int addCount = 0;
    LinearLayout mLinearLayout = binding.linearLayoutId;
    for (int i = 0; i < 5; i++) {
        LinearLayout newLayout = new LinearLayout(getActivity());
        newLayout.setBackground(new ColorDrawable(0xFFAAAAAA));
        TextView tv = new TextView(getActivity());
        tv.setTextSize(24);
        tv.setText("Hello World! " + ++addCount);
        newLayout.addView(tv);
        Button button = new Button(getActivity());
        button.setText("Click me");
        newLayout.addView(button);
        mLinearLayout.addView(newLayout);
        ((LinearLayout.MarginLayoutParams) newLayout.getLayoutParams()).setMargins(margin, margin, margin, margin);
        button.setOnClickListener(v -> { // v is the button
            mLinearLayout.removeView((ViewGroup) v.getParent());
        });
    }
    

    顶部布局很简单

    <LinearLayout
        android:id="@+id/linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:orientation="vertical"/>
    

    在下文中,我只需单击奇数布局的按钮,然后单击偶数布局的按钮。

    【讨论】:

      【解决方案2】:

      这是因为您要删除整个布局,而不是应该删除动态添加的布局 我假设删除产品是 product_layout 主视图组中的直接子项

          removeProduct.setOnClickListener(v -> {
         if (mLinearLayout.getChildCount()>0)
         {
             ((LinearLayout)mLinearLayout.getParent()).removeView(removeProduct.getParent());
         }
      
      });}
      

      但是您应该记住,这完全不推荐,您应该使用 recyclerview 并在添加新产品时将对象添加到产品列表并调用 notifyDataSetChanged(),您也可以使用 System.currentTimeMillis() 作为产品的唯一 ID目的。

      【讨论】:

      • removeProduct 是按钮名称,通过 removeProduct.getParent() 它将仅删除按钮。
      • 不,它会删除产品布局
      • 好的,我现在收到 incompatible types: ViewParent cannot be converted to View 错误
      • 我正在使用片段
      • 可以在github上分享代码吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      相关资源
      最近更新 更多