1.使用软键盘上的搜索按钮,可以在布局中或代码中配置
1>在布局中
android:imeOptions="actionSearch"
android:singleLine="true"
2>在代码中配置
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
2.设置事件监听
/**
* 键盘上的Search button事件
*/
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_SEARCH)) {
startSearch();
return true;
}
return false;
}
});
3.Edittext中内容变化监听
/**
* 监听edittext中内容的变化
*/
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() > 0) {
ivDelete.setVisibility(View.VISIBLE);
} else {
ivDelete.setVisibility(View.GONE);
}
}
});
4,将editext的光标移到最后
//将光标移动需要输入文本的地方
Editable etext = editText.getText();
Selection.setSelection(etext, etext.length());
5.点击输入法边缘其他地方隐藏输入法
/**
* 隐藏输入法
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.getCurrentFocus() != null) {
InputMethodManager mInputMethodManager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
return mInputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
return super.onTouchEvent(event);
}
6.编辑框点击之后才弹出软键盘
/**
* 编辑框点击之后才弹出软键盘,所以在进入页面的时候设置
*/
public static void setSoftInputMode(Context context) {
if (context instanceof Activity) {
Activity activity = (Activity) context;
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
}
7.密码的显示和隐藏
//设置密码为隐藏的
mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
mEtEnsuerPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());