2016-11-24 40 views
0

我有一個自動完成editText字段,我發現這個代碼用於清除editText字段上的文本,並將它放在字段的右上角,我的問題是在editText字段上清除一次文本後十字(drawable)消失。我必須離開頁面並再次返回,以便它再次可見。我怎樣才能讓它始終可見?請幫助。 這是我的代碼有:清除editText字段wth可繪製

String value = ""; 
    personAccountableAutoCompleteTextView.setText(value); 
    final Drawable x = getResources().getDrawable(R.drawable.clear); 
    personAccountableAutoCompleteTextView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (personAccountableAutoCompleteTextView.getCompoundDrawables()[2] == null) { 
       return false; 
      } 

      if (event.getX() > personAccountableAutoCompleteTextView.getWidth() - personAccountableAutoCompleteTextView.getPaddingRight() - x.getIntrinsicWidth()) { 
       personAccountableAutoCompleteTextView.setText(""); 
       x.setVisible(true,true); 
       personAccountableAutoCompleteTextView.setCompoundDrawables(null, null, null, null); 
      } 
      return false; 
     } 
    }); 

回答

0

給YOUT EditText上繪製權在這樣的XML:

<EditText 
    android:layoutheight="wrap_content" 
    android:layoutwidth="match_parent" 
    android:drawableright="@drawable/yourimg"/> 

,然後給點擊聽者到繪製這樣的:

edittext.setOnTouchListener(new 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() >= (edittext.getRight() - edittext.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 
        // your action here 

       return true; 
       } 
      } 
      return false; 
     } 
    }); 
+0

我得到的那部分是正確的,我的問題是drawable消失後,我使用它一次,我無法清除EditText字段兩次。 – Fuluza

+0

它不應該使用上面的代碼消失! –

+0

非常感謝Satish :)這工作! – Fuluza