0

I have an edit text, when I enter something here and tap outside, the focus and the softkey board should hide and change the visibility of a button if the value entered is not empty. If I again enter and change the value of the edit text 0 and tap out, the button should become invisible again. PFA what I'm doing:

public void onFocusChange(View v, boolean hasFocus) {
    String s = enter_num.getText().toString();
    if(!(s.equals(""))){
        enter_num.setFocusable(isFinishing());
        InputMethodManager imm = (InputMethodManager).getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);                         
        ans_status2.setVisibility(View.VISIBLE);                                                                
    }
}

This is not losing the focus. Is this the right approach to start with?

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
bharath
  • 953
  • 4
  • 17
  • 30

1 Answers1

0

Try this as:

public void onFocusChange(View v, boolean hasFocus) {
    String s = enter_num.getText().toString().concat("");
    int length = s.length();
    if(length <= 0){                
    } else {
        if(s.equals("0")){
            enter_num.setFocusable(isFinishing());
            InputMethodManager imm = (InputMethodManager).getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);                         
            ans_status2.setVisibility(View.VISIBLE);                                                                
         }
    }
}

Hope this will help you.

Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95
  • I think it should be length!=0, but that is not the problem, on clicking at any place other than the edit text, i want the keyboard and the focus for the edit text to go, that is not happening with onfocuschange! – bharath Mar 27 '13 at 12:45