2016-10-04 74 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