2016-10-04 93 views
1

的繪製右鍵單擊事件我已經加入十字圖標到EDITTEXT繪製權,我想只顯示可繪製右邊的圖標,當我進入的EditText的東西,我想在十字圖標進行點擊事件,如果有它應該刪除任何輸入的文本。我該怎麼做 ?問題而實施的EditText

+0

'Drawable's沒有收到任何事件,文檔說:'「」「不像一個視圖,可拉伸沒有任何設施,以接收事件或以其他方式與用戶進行交互。」「」' – pskink

+0

可能[重複](http://stackoverflow.com/questions/3554377/handling-click-events-on-a-drawable-within-an-edittext) –

+0

我試過這些鏈接,我可以執行點擊事件它,但我不想顯示可繪製的左側圖標,如果沒有任何輸入的文本 –

回答

1

XML代碼`

    <EditText 
         android:id="@+id/et_text" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:ems="12" 
         android:hint="Text" 
         android:singleLine="true" /> 

       <ImageButton 
        android:id="@+id/ib_clear" 
        style="?android:buttonBarButtonStyle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="-50dp" 
        android:layout_marginStart="-50dp" 
        android:src="@drawable/ic_cancel_black_24dp" /> 
      </LinearLayout>` 

您可以在Java代碼中更改十字圖標(圖像按鈕)的可見性。你可以添加onclicklistener圖片按鈕clickevent。

千萬以下隱藏或顯示十字圖標

et_text.addTextChangedListener(new TextWatcher(){ 
public void afterTextChanged(Editable s) {} 
public void beforeTextChanged(CharSequence s, int start, int count, int after){} 
public void onTextChanged(CharSequence s, int start, int before, int count){if(et_text.getText().toString().length()>0){et_text.setVisibility(VIEW.VISIBLE);}else{et_text.setVisibility(VIEW.GONE);} 
}}); 
+0

完美,非常感謝,它工作:-) –

+0

不客氣☺ –

0

你不能讓drawble監聽事件中的drawble左,右drawble情況。然而,動態使用它可以幫助。 所以,你需要做的就是添加textwatcher您editext和B之後文本

改變監聽你的editext的你需要隱藏和顯示你的drawble左 ,右動態 。 添加動態drawble:

Drawable icon = getContext().getResources().getDrawable(R.drawable.smiley); 

    icon.setBounds(0, 0, 60, 60); 
editext.setCompoundDrawables(icon, null, null, null); 

Drawable img = getContext().getResources().getDrawable(R.drawable.smiley); 

editext.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null); 

editext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley,0,0,0);

1

上添加點擊監聽,爲您繪製的右下面的代碼將幫助您:

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

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

我們getRawX(),因爲我們想觸摸的屏幕上不是相對於母公司的實際位置。

若要左側點擊

if(event.getRawX() <= (editComment.getCompoundDrawables()[DRAWABLE_LEFT].getBounds().width())) 

更新:使繪製右可見,如果在編輯文本輸入一些字符,然後下面的代碼將工作:

edittext.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     // TODO Auto-generated method stub 

     if(edittext_search.getText().length()>0) 
     { 
      // Add drawable Right 
     edittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon, 0); 

     } 
     else 
     { 
      edittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); 
     } 

    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     // TODO Auto-generated method stub 

    } 
}); 
+0

我可以執行它的點擊事件,我可以刪除任何輸入的文本,但我的問題是,如果edittext是空的,那麼可繪製的權利應該隱藏,儘快啓動它應該是可見的並且能夠執行單擊事件 –

+0

更新了edittext更改偵聽器 – Anjali

0
<RelativeLayout 
android:id="@+id/mainLayout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

    <LinearLayout 
    android:id="@+id/linearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_toLeftOf="@+id/crossButton" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/editTextField" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 
    </RelativeLayout> 
</LinearLayout> 

<ImageButton 
    android:id="@+id/crossButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:visibility="gone" /> 
</RelativeLayout> 

在創建XML像上面,你必須的EditText和圖像後;我還設置了您的密切圖像可見性消失。現在內部代碼實現FocusChangeListener,您將在輸入文本時啓用按鈕可見性。

editTextField.setOnFocusChangeListener(new OnFocusChangeListener(){ 
@Override 
     public void onFocusChange(View view, boolean hasFocus) { 
      if(hasFocus && editTextField.getText().toString().length() > 0){ 
       crossButton.setVisibility(View.VISIBLE); 
      } 
      else{ 
       crossButton.setVisibility(View.GONE); 
      } 

      crossButton.invalidate(); 
     } 
    }); 

我寫了一個示例代碼供您參考,希望這會回答您的查詢。

  • 一個XML具有的EditText與圖像按鈕。

  • 使這個xml膨脹並應用代碼來啓用/禁用可見性狀態邏輯。

+0

謝謝你的回覆,它的工作 –

+0

@TaraUppin太棒了!乾杯:)是的,別忘了接受答案。 – Anurag