5

我知道這裏有很多關於這個問題的問題。但他們中沒有人有我正在尋找的答案。當軟鍵盤顯示時按下後退按鈕時清除焦點EditText

我在ScrollView中有7個ET。當我啓動應用程序沒有ET具有焦點,因爲我已經添加了以下兩行到我的總體佈局:

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

當我點擊一個ET顯示在softkeyboard,這是我想要的,那麼我設置的值(比如說20)。我按'2'然後按'0',然後按後退按鈕。此時鍵盤消失,但焦點保持不變。按下後退按鈕隱藏鍵盤時,我也想清除焦點。

因爲重點被清除時,ET的佈局就像我想要的那樣設置。

他們都有關於一些代碼,這就是:

// 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); 
      } 
     } 
    }); 

我用一個焦點監聽到ET的輸入更改爲一個數字,如5.00而不是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"); 
       } 
      } 
     } 
    } 

}; 
+0

你找到了一個解決方案? – PrisonMike

+0

我強烈建議在附加鏈接Kacy答案: http://stackoverflow.com/questions/13975058/make-edittext-lose-focus-on-back-press –

回答

0

爲此,您必須在佈局文件的父佈局上採用onTouchListener。在TouchListener上,當在EditText外單擊時,必須編碼以隱藏鍵盤。請遵循以下XML佈局和Java類來解決此問題。

請遵循遵循以下URL來解決這個問題http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html

+0

如果您有任何疑問,請發表在我的博客或評論它 –

+0

謝謝你很高興知道,但不幸的是不是我正在尋找。 – patrick

+0

你想要的是,如果我按下後退鍵,那麼鍵盤將不可見,並且重點放在編輯文本上是釋放。對? –

相關問題