一自動獲取焦點,並且彈出輸入法。
et_label_name = (EditText) view.findViewById(R.id.et_label_name);
et_label_name
.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// 此處爲得到焦點時的處理內容
UIHelper.showInputMethodFromView(getContext(), et_label_name);
} else {
// 此處爲失去焦點時的處理內容
<span style="white-space:pre"> </span>UIHelper.hideSoftInputFromWindow(et_label_name);
}
}
});
獲取焦點
et_label_name.requestFocus();
二輸入法工具
/**
* 隱藏輸入法
*
* @param view
*/
public static void hideSoftInputFromWindow(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 顯示鍵盤
* @param context
* @param view
*/
public static void showInputMethodFromView(Context context, View view) {
InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(view, 0);
}