Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

editText is not losing focus

the editText is not losing focus in my app when I click out of it, it has the orange border all time and that black line cursor.. I did this a LinearLayout according to this surrounding the editText: Stop EditText from gaining focus at Activity startup so it doesn't get focus on start of the app..

this is my code:

final EditText et = (EditText)findViewById(R.id.EditText01);

final InputMethodManager imm = (InputMethodManager) getSystemService(
   INPUT_METHOD_SERVICE);

et.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
      imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
   }
});

et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
   public void onFocusChange(View v, boolean hasfocus) {
      if(hasfocus) {
         imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
      } else {
         imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
      }
   }
});

But, it doesn't seem to be calling the onFocusChange when I click anywhere outside the editText!

like image 992
mnio Avatar asked Dec 20 '10 12:12

mnio


People also ask

How do I know if EditText has focus?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class.


1 Answers

It's simple. You can put the following in LinearLayout or any other else:

android:focusableInTouchMode="true"
like image 74
PPShein Avatar answered Oct 20 '22 14:10

PPShein