0

EditText里面有清除按钮:

<FrameLayout
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="0dp"
    android:padding="5dp" >

    <EditText
        android:id="@+id/etSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="Enter word here"
        android:singleLine="true"
        android:textSize="20dp" />

    <Button
        android:id="@+id/bClearText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_marginRight="10dp"
        android:onClick="clearEtSearch"
        android:background="@drawable/delete" />
</FrameLayout>

MainActivity 类中的 TouchDelegate 代码:

View parent = findViewById(R.id.root);
    bClearText = (Button) findViewById(R.id.bClearText);
    parent.post(new Runnable() {
        public void run() {
            // Post in the parent's message queue to make sure the parent
            // lays out its children before we call getHitRect()
            Rect delegateArea = new Rect();
            Button delegate = bClearText;
            delegate.getHitRect(delegateArea);
            delegateArea.top -= 100;
            delegateArea.bottom += 100;
            delegateArea.left -= 100;
            delegateArea.right += 100;
            TouchDelegate expandedArea = new TouchDelegate(delegateArea,
                    delegate);
            // give the delegate to an ancestor of the view we're
            // delegating the
            // area to
            if (View.class.isInstance(delegate.getParent())) {
                ((View) delegate.getParent())
                        .setTouchDelegate(expandedArea);
            }
        };

    });

但是 TouchDelegate 不起作用。触控区域未扩大。按钮仍然很难被点击。我做错了什么?

删除按钮图像:

在此处输入图像描述

4

0 回答 0