画面表示時にソフトキーボードを表示させない
参照:https://qiita.com/hishida/items/8bf1aa28ef62ef22a7f9
下線を非表示にする
android:background="#00000000"
を属性に追加
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:hint="何をお探しですか?"/>
EditText以外をタップされたらソフトキーボードを非表示にする
ActivityでdispatchTouchEvent()
をオーバーライドする
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
val v = currentFocus
if (v is EditText) {
val outRect = Rect()
v.getGlobalVisibleRect(outRect)
if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
v.clearFocus()
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(v.windowToken, 0)
return false
}
}
}
return super.dispatchTouchEvent(event)
}
フォーカスされたら自動でソフトキーボードを表示する
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
参考:Show soft keyboard automatically when EditText receives focus
キーボードの検索ボタンタップを検知する
EditTextにimeOptions
、inputType
を以下の様に設定
<EditText
android:imeOptions="actionSearch"
android:inputType="text"/>
EditTextにリスナーをセット
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Your piece of code on keyboard search click
return true;
}
return false;
}
});