16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EditTextメモ

Last updated at Posted at 2017-12-20

画面表示時にソフトキーボードを表示させない

参照: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にimeOptionsinputTypeを以下の様に設定

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

参考:detect keyboard search button

16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?