5

I know that there are quite some questions out here regarding this question. But non of them have the answer that I'm looking for.

I've got 7 ET inside a ScrollView. When I start the application no ET has focus because I've added the following two lines to my overall layout:

android:focusable="true"
android:focusableInTouchMode="true"

When I click on an ET the softkeyboard is shown, which I want, then I set the value (let say 20). I press a '2' followed by a '0' and then press the back button. At this point the keyboard disappears, but the focus stays. I would like to clear the focus too when pressing the back button to hide the keyboard.

Because when the focus is cleared the layout of the ET is set like I want.

They all have about the some code, which is:

    // Right Cable
    RightCable = (EditText) findViewById (R.id.RightCable);
    RightCable.setRawInputType(Configuration.KEYBOARD_12KEY);
    RightCable.setOnFocusChangeListener(FocusChanged);
    RightCable.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            if(RightCable.isFocused()){
                LengthRightCable       = Double.parseDouble(RightCable.getText().toString());
                Calculate();
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(s.toString().matches("")) {
                RightCable.setText("0.00");
                Selection.setSelection(RightCable.getText(), 0, 4);
            }
        }
    });

I use a focus listener to change the input of the ET to a number like 5.00 instead of 0.

OnFocusChangeListener FocusChanged = new OnFocusChangeListener() {

    public void onFocusChange(View v, boolean hasFocus) {

        EditText et = (EditText) v;

        et.setSelection(0, et.getText().length());

        if(!hasFocus){
            String userInput = et.getText().toString();

            int dotPos = -1;    

            for (int i = 0; i < userInput.length(); i++) {
                char c = userInput.charAt(i);
                if (c == '.') {
                    dotPos = i;
                }
            }

            if (dotPos == -1){
                et.setText(userInput + ".00");
            } else if(userInput.length() < 5) {
                if ( userInput.length() - dotPos == 1 ) {
                    et.setText(userInput + "00");
                } else if ( userInput.length() - dotPos == 2 ) {
                    et.setText(userInput + "0");
                }
            }
        }
    }

};
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
patrick
  • 1,282
  • 4
  • 21
  • 37

2 Answers2

1
 Just override `onBackPressed()` 

method in your activity and check condition for Edittext focus before run super.onBackPressed();

 @Override
            public void onBackPressed() {
        
                if( et_search.isFocused()){
                    et_search.clearFocus();
                }else {
                    super.onBackPressed();
                }
            }
HiPradeep
  • 81
  • 9
0

For this you have to take the onTouchListener on the parent layout of the Layout File. on the TouchListener you have to code to hide the Keyboard when click outside the EditText. Please follow following XML Layout and Java class to resolve this issue.

Please follow the follow the following url to resolve this issue http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html

Amit Thaper
  • 2,117
  • 4
  • 26
  • 49