本文介绍了EditText的drawableRight上的Click事件不能正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要单击EditTextdrawableRight打开电话的通讯录. drawableRight上的单击事件运行正常,但是问题是,当我单击/触摸EditText上的任何位置时,它也执行单击事件并打开联系人列表.

I need to open phone's contact book on the click of EditText's drawableRight. Click event on drawableRight is working fine But the problem is, when I click/touch on anywhere on EditText it is also execute click event and open contact list.

我从这里drawableRight上的点击事件>请检查此链接.

I take help for manage click event on drawableRight from here Please check this link.

当我单击EditText时,我不想打开联系人列表,我只想单击drawableRight(图像)时打开它.那么如何解决这个问题呢?

I don't want to open contact list when I click on EditText, I only want to open it when I click drawableRight (image). So how solve this problem?

这是我的代码:

EditText mobile_number;
mobile_number = (EditText)view.findViewById(R.id.mobile_number1);
mobile_number.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;
                if(event.getAction()==MotionEvent.ACTION_UP){

                    if(event.getRawX()>=(mobile_number.getRight()-mobile_number.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width()));
                    {

                         Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                        startActivityForResult(intent,PICK_CONTACT);

                        return true;
                    }
                }
                return true;
            }
        });

这是我的布局代码:

<LinearLayout
        android:layout_width="fill_parent"
        android:id="@+id/linearlayout_two1"
        android:layout_below="@+id/linearlayout_one1"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:orientation="vertical"
        >

        <EditText
            android:layout_width="300dp"
            android:hint="Enter Your Mobile Number"
            android:textSize="15sp"
            android:id="@+id/mobile_number1"
            android:paddingLeft="30dp"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/editbox_icon"
            />
</LinearLayout>

推荐答案

而不是使用getRawX(),请尝试将该行替换为

Instead of using getRawX(), try replacing that line with

if (event.getX() >= (mobile_number.getWidth() - mobile_number
        .getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {

我相信View.getRight()返回相对于其父视图的右边缘的位置,而TouchEvent.getRawX()返回屏幕上的绝对X位置.

I believe View.getRight() returns the position of the right edge of the View relative to its parent, while TouchEvent.getRawX() returns the absolute X position on the screen.

再次编辑以演示我的观点:

EDIT AGAIN TO DEMONSTRATE MY POINT:

MainActivity.xml

MainActivity.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.meremammal.www.edittextdrawable.MainActivity">

    <!-- This layout is only here to demonstrate a situation that breaks the usage of getRawX() -->
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true">

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="400dp"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:drawableRight="@android:drawable/ic_input_add"/>

    </RelativeLayout>

</RelativeLayout>

MainActivity.java

MainActivity.java

public class MainActivity extends AppCompatActivity {

    EditText mEditText;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = this;
        mEditText = (EditText) findViewById(R.id.edit_text);
        mEditText.setOnTouchListener(new View.OnTouchListener() {

            private float touchX = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int drawableLeft = mEditText.getRight() - mEditText
                        .getCompoundDrawables()[2].getBounds().width();
                // This detects the location of touch on ACTION_DOWN, but because it is
                // using getRawX() and getRight() and the EditText's parent is not at the
                // left of the screen, it will respond when clicked in the middle of the
                // EditText. Instead, use getX() and EditText.getWidth()
                if (event.getAction() == MotionEvent.ACTION_DOWN && event.getRawX() >= drawableLeft) {
                    touchX = event.getRawX();
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP && touchX >= drawableLeft) {
                    Toast.makeText(mContext, "Clicked Button",     Toast.LENGTH_SHORT).show();
                    touchX = 0;
                    return true;
                } else {
                    return mEditText.onTouchEvent(event);
                }
            }
        });
    }
}

这篇关于EditText的drawableRight上的Click事件不能正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 17:12