سؤال

I have a TextView that converts to an EditView on touch. However, I want the opposite to occur when a user touches anywhere outside of the EditView. I've tried to use setOnFocusChangeListener, but that only appears to work when something else gains focus. I know I can intercept onTouch and check there, but is there a simpler way to handle this scenario?

هل كانت مفيدة؟

المحلول

Set parent of TextView and edit view to focusable.

Make all layouts and views clickable, and implement onClickListener on each, write onClickListener like below:

private ClickListener implements OnClickListener
{
    public void onClick(View view)
    {
         if(v.getId()!=R.id.editView)
         {
              //Write your code here to replace views
         }
    }
}

نصائح أخرى

i have used this in my project too and have solved this as follows:

i set the layout outside from EditText as "focusable=true" and "clickable=true", the EditText have no background so it looks like a TextView, on focus change i change the text color of EditText so the user can see he/she can edit the text. then, the layout outside have a OnClickListener, on click i hide the software keyboard and change the text color back to the TextView color. This all is working without to switch elements. If you want, you can place both the elements in a FrameLayout to make sure the elements have the same positions in the layout, implement a OnClickListener on the TextView, in the onClick event you then switch the Objects visibility.

maybe this idea helps you ;)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top