当软键盘显示时按下后退按钮时清除焦点EditText
我知道这里有很多关于这个问题的问题。但他们中没有人有我正在寻找的答案。当软键盘显示时按下后退按钮时清除焦点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");
}
}
}
}
};
为此,您必须在布局文件的父布局上采用onTouchListener。在TouchListener上,当在EditText外单击时,必须编码以隐藏键盘。请遵循以下XML布局和Java类来解决此问题。
请遵循遵循以下URL来解决这个问题http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html
如果您有任何疑问,请发表在我的博客或评论它 –
谢谢你很高兴知道,但不幸的是不是我正在寻找。 – patrick
你想要的是,如果我按下后退键,那么键盘将不可见,并且重点放在编辑文本上是释放。对? –
你找到了一个解决方案? – PrisonMike
我强烈建议在附加链接Kacy答案: http://stackoverflow.com/questions/13975058/make-edittext-lose-focus-on-back-press –