【问题标题】:Android : flowlayout inner button with close icon onclick eventAndroid:带有关闭图标onclick事件的flowlayout内部按钮
【发布时间】:2015-12-10 10:49:26
【问题描述】:

我需要创建一个带有关闭图标的按钮,如下所示:

我有使用按钮,当我点击按钮删除按钮时出现问题。

但我的要求是删除表单列表时单击关闭图标。

所以请帮助我使用哪个控件来解决我的问题。

【问题讨论】:

    标签: android flowlayout


    【解决方案1】:

    使用此代码将解决您的问题

    buttondelete.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
                final int DRAWABLE_RIGHT = 2;
    
    
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    if(event.getRawX() >= (buttondelete.getRight() - buttondelete.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        // your action here
    
                     return true;
                    }
                }
                return false;
            }
        });
    

    【讨论】:

    • 投票有用..谢谢..我想这就是我想要的。
    【解决方案2】:

    我将添加到流布局 customLaout,其中包含灰色背景和线性布局,内部有 2 个元素。 TextView 和 ImageView。

    两者都有自己的 ClickListeners

    类似:

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:gravity="center"
            android:layout_gravity="center"
            android:text="Example Text"
            android:id="@+id/buttonTextView"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_gravity="center"
            android:id="@+id/buttonExitIcon"
            android:src="@drawable/your_image_source"
            />
    
    </LinearLayout>
    

    并为 imageView 和 TextView 添加点击监听器

    【讨论】:

      【解决方案3】:
      {
           RelativeLayout mainView = (RelativeLayout)findViewById(R.id.item);
           View child = getLayoutInflater().inflate(R.layout.child, null);
           Button bt_close = (Button)child.findviewById(R.id.btnclose);
           bt_put.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                 RelativeLayout pView = (RelativeLayout)v.getParent();
                 mainView.removeView(pView); 
              }
          });
           mainView.addView(child);
       }
      
      1. 使用带子项的 RelativeLayout 创建一个 child.xml(TextView 用于文本,Button 用作删除按钮)

      2. 膨胀 xml 布局,将每个视图添加到您的 main_layout 和 setonclicklistner 到按钮。

      3. 单击按钮时,从单击的视图中获取父视图并从 main_layout 中删除该视图。

      这可能会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 2022-01-15
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2015-06-25
        • 2014-04-04
        • 2013-02-28
        • 1970-01-01
        • 2019-11-14
        相关资源
        最近更新 更多