的繪製右鍵單擊事件我已經加入十字圖標到EDITTEXT繪製權,我想只顯示可繪製右邊的圖標,當我進入的EditText的東西,我想在十字圖標進行點擊事件,如果有它應該刪除任何輸入的文本。我該怎麼做 ?問題而實施的EditText
回答
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);}
}});
完美,非常感謝,它工作:-) –
不客氣☺ –
你不能讓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);
上添加點擊監聽,爲您繪製的右下面的代碼將幫助您:
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
}
});
我可以執行它的點擊事件,我可以刪除任何輸入的文本,但我的問題是,如果edittext是空的,那麼可繪製的權利應該隱藏,儘快啓動它應該是可見的並且能夠執行單擊事件 –
更新了edittext更改偵聽器 – Anjali
<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膨脹並應用代碼來啓用/禁用可見性狀態邏輯。
謝謝你的回覆,它的工作 –
@TaraUppin太棒了!乾杯:)是的,別忘了接受答案。 – Anurag
- 1. 問題而實施軍刀車API
- 2. UIMapkit實施問題
- 3. Backprop實施問題
- 4. 問題實施的JFreeChart
- 5. 實施Adwhirl的問題
- 6. iAd實施的問題?
- 7. 問題實施IRenderingExtesion接口
- 8. 問題實施模型
- 9. 問題,以DCT實施
- 10. 實施typeahead.js CSS問題
- 11. 問題與實施Adwhirl
- 12. SharePoint 2016實施問題
- 13. 我而實施的標籤有問題的Android
- 14. 文檔轉換實時 - 實施問題
- 15. 的EditText問題:
- 16. 的EditText問題
- 17. 問題實施利亞的PullRefreshTableViewController
- 18. 實施PHP Thrift服務器的問題
- 19. 與RBAC實施相關的問題
- 20. 問題實施的UIView類別
- 21. 從零開始實施BlockingQueue的問題
- 22. 實施滑動輪播的問題
- 23. 實施角度服務的問題
- 24. 由於投射問題而試圖獲取EditText的問題
- 25. EditText問題
- 26. EditText問題
- 27. EDITTEXT lineSpacingExtra問題
- 28. EditText AfterTextChanged問題
- 29. EditText TextChangeListener問題
- 30. 關於內容管理系統實施的問題/問題
'Drawable's沒有收到任何事件,文檔說:'「」「不像一個視圖,可拉伸沒有任何設施,以接收事件或以其他方式與用戶進行交互。」「」' – pskink
可能[重複](http://stackoverflow.com/questions/3554377/handling-click-events-on-a-drawable-within-an-edittext) –
我試過這些鏈接,我可以執行點擊事件它,但我不想顯示可繪製的左側圖標,如果沒有任何輸入的文本 –