总结一下最近用到的知识点。参考了很多人的资料这里不一一列出了,十分感谢。
一、editText默认设置不选中
在xml中在EditText父节点增加
android:focusable="true"
android:focusableInTouchMode="true" 表示将焦点给EditText的父节点。
二、editText主动失去焦点
EditText editText;
editText.clearFocus()
三、editText的文字变化监听
editText.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
temp = s;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
以上是最基本格式,实际情况可以重写TextWatcher。
四、点击空白处隐藏软键盘
// 获取点击事件
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View view = getCurrentFocus();
if (isHideInput(view, ev)) {
HideSoftInput(view.getWindowToken());
clearAllFocus();
}
}
return super.dispatchTouchEvent(ev);
}
// 判定是否需要隐藏
private boolean isHideInput(View v, MotionEvent ev) {
if (v != null && (v instanceof EditText)) {
int[] l = { 0, 0 };
v.getLocationInWindow(l);
int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
+ v.getWidth();
if (ev.getX() > left && ev.getX() < right && ev.getY() > top
&& ev.getY() < bottom) {
return false;
} else {
return true;
}
}
return false;
}
// 隐藏软键盘
private void HideSoftInput(IBinder token) {
if (token != null) {
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(token,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
五、改变enter键的属性
private OnKeyListener onKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
/*隐藏软键盘*/
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(inputMethodManager.isActive()){
inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
return true;
}
return false;
}
};
我这里按回车键的反馈是隐藏键盘。根据实际情况修改或者改写,之后调用
edittext.setOnKeyListener(onKeyListener);
精确的判断右下角按键情况,以便应对更加复杂的情况。
edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
/*判断是否是“GO”键*/
if(actionId == EditorInfo.IME_ACTION_GO){
/*隐藏软键盘*/
InputMethodManager imm = (InputMethodManager) v
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(
v.getApplicationWindowToken(), 0);
}
return true;
}
return false;
}
});
actionNone : 回车键,按下后光标到下一行
actionGo : Go,actionSearch : 放大镜
actionSend : Send
actionNext : Next
actionDone : Done,确定/完成,隐藏软键盘,即使不是最后一个文本输入框
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionSearch"/>
改变方法就是给EditText控件的imeOptions属性设置成不同的值(此时Enter键可以显示不同的文字和图案)。
以上,后续再碰到其他问题还会继续修改。