我知道這裏有很多關於這個問題的問題。但他們中沒有人有我正在尋找的答案。當軟鍵盤顯示時按下後退按鈕時清除焦點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");
}
}
}
}
};
你找到了一個解決方案? – PrisonMike
我強烈建議在附加鏈接Kacy答案: http://stackoverflow.com/questions/13975058/make-edittext-lose-focus-on-back-press –