2

我需要打開EditTextdrawableRight點擊電話的聯繫簿。 drawableRight上的點擊事件工作正常但問題是,當我點擊/觸摸EditText上的任何地方時,它也是執行點擊事件並打開聯繫人列表。EditText的drawableRight上的Click事件無法正常工作?

我採取幫助從這裏Please check this link.

drawableRight管理單擊事件我不想打開聯繫人列表當我點擊EditText,我只是想打開它,當我點擊drawableRight(圖像)。那麼如何解決這個問題呢?

drawbleRight

這裏是我的代碼:

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

添加到佈局1是編輯文本,另一個是圖像。然後在圖像中設置ontuchlistner \ – Destro

+0

將最後一個'返回true;'更改爲'return false;',然後再次測試。 – NamNH

+0

@John將它改爲false不起作用,任何事情........我已經試過了。 –

回答

0

您沒有獲得正確的圖像,據我所知,除非您創建自定義EditText類。我建議使用RelativeLayout的,有一個EDITTEXT和一個ImageView的,並設置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> 
0

做一個線性佈局,水平方向,並添加編輯文本和圖像的視圖適當重..

上點擊單獨的每個項目,則向.....

4

而不是使用getRawX()的,嘗試替換該行

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

編輯:我相信View.getRight()返回視圖的右邊緣相對於其父的位置,而TouchEvent.getRawX()返回屏幕上的絕對X位置。

編輯再次證明了我的觀點:

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

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

是啊........我錯誤地在if語句的末尾加上了分號....... OOPs ........可能是IDE應該爲這種類型的未成年人提供警告或其他東西錯誤。我的錯。 –

+0

這是否意味着您的現有條件可以在沒有分號的情況下運行? –

+1

我認爲這個問題的問題是''if'語句末尾的冗餘分號';'。而不是'getX()'或'getRawX()' – NamNH

0

就恰克最後returnfalse。您還必須在if語句結束時刪除逗號;