简体   繁体   English

EditText的drawableRight上的Click事件不能正常工作吗?

[英]Click event on EditText's drawableRight not working properly?

I need to open phone's contact book on the click of EditText 's drawableRight . 我需要单击EditTextdrawableRight打开电话的通讯录。 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上的单击事件工作正常,但是问题是,当我在EditText上的任何位置单击/触摸时,它也执行单击事件并打开联系人列表。

I take help for manage click event on drawableRight from here Please check this link. 我从这里获得有关管理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). 当我单击EditText ,我不想打开联系人列表,我只想在单击drawableRight (图像)时打开它。 So how solve this problem? 那么如何解决这个问题呢?

drawbleRight

Here is my code: 这是我的代码:

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

Here is my layout code: 这是我的布局代码:

<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>

Instead of using getRawX(), try replacing that line with 而不是使用getRawX(),请尝试将该行替换为

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

EDIT: 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. 编辑:我相信View.getRight()返回相对于其父视图的右边缘的位置,而TouchEvent.getRawX()返回屏幕上的绝对X位置。

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

You don't have access to the right image as far my knowledge, unless you create custom EditText class. 据我所知,除非您创建自定义EditText类,否则您将无法访问正确的图像。 I suggest to use a RelativeLayout, with one editText and one imageView, and set OnClickListener over the image view as below: 我建议使用带有一个editText和一个imageView的RelativeLayout,并在图像视图上设置OnClickListener,如下所示:

<RelativeLayout
        android:id="@+id/rlSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/edit_text"
        android:padding="5dip" >

        <EditText
            android:id="@+id/txtSearch"
            android:layout_width="match_parent"

            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/imgSearch"
            android:background="#00000000"
            android:ems="10"/>

        <ImageView
            android:id="@+id/imgSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/btnsearch" />
    </RelativeLayout>

Make a Linear layout with horizontal orientation and add a edit-text and image-view with proper weight.. 制作水平方向的线性布局,并添加适当权重的编辑文本和图像视图。

Then Give on click for each item separately..... 然后分别点击每个项目。

Just chage the last return to false . 只是责怪最后一次return false Also you have to remove the comma ; 另外,您必须删除逗号; at the end of if statement. 在if语句的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM