android之EditText焦點問題

一自動獲取焦點,並且彈出輸入法。

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);
	}

發佈了83 篇原創文章 · 獲贊 17 · 訪問量 103萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章