Android:flowlayout内部按钮,带有关闭图标的onclick事件

时间:2015-12-10 10:49:26

标签: android flowlayout

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

enter image description here

我有使用按钮,当我点击按钮删除按钮时我遇到了问题。

但删除表单列表时我的要求是onclick close图标。

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

3 个答案:

答案 0 :(得分:0)

使用此代码可以解决您的问题

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;
        }
    });

答案 1 :(得分:0)

我会添加流布局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添加Click侦听器

答案 2 :(得分:0)

{
     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中删除视图。

  4. 这可能会对你有帮助。

相关问题