简体   繁体   中英

OnTouchListener is not working in EditText when no drawable is present in android application?

I have an EditText field that is used for searching purpose. Now my requirement is to set clear icon on drawableRight when any text is present in textbox and hide drawableRight icon when there is no text present. In this EditText I have implemented two Listener :

  1. setOnTouchListener

     protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_ticket); txtBookingCode.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); txtBookingCode.setOnTouchListener(this); } 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() >= (txtBookingCode.getRight() - txtBookingCode.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { txtBookingCode.setText(""); return true; } } return false; } 
  2. addTextChangedListener

     txtBookingCode.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { if(!s.toString().isEmpty()) txtBookingCode.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_clear_white, 0); else txtBookingCode.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { } }); 

Now, first time no clear icon is shown here because there is no any text in textbox. Now if I touch inside the textbox to type searching data the app throws an error and app is closed. If I don't hide the clear icon then touching the textbox, no error is occurred.

I reached at final conclusion that if clear icon is there then no error otherwise error on touching textbox(anywhere inside).

I am also putting xml code below.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/dp_30"
        android:theme="@style/SettingTextLabel"
        app:hintTextAppearance="@style/SettingTextLabel">

        <EditText
            android:id="@+id/txtBookingCode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_clear_white"
            android:textColor="@color/colorWhite"
            android:textSize="@dimen/txt_20sp"
            android:hint="@string/hint_Search" />
    </android.support.design.widget.TextInputLayout>

</LinearLayout>

Update :

I got log error :

W/InputDispatcher: channel 'f2c62c8 com.posorbis.ticketscan/com.posorbis.ticketscan.SearchTicketActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9

A simple textwatcher is fine. Do your code in afterTextChanged method. If you put some Log methods in the textwatcher override methods you will find that they are called quite a lot during text input so they might be interfering with each other.

Keep all methods empty except afterTextChanged and see what effect that has on your icons. Then go from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM