文字入力時、それ以外のViewにフィルターをかける【Android】
こんにちは。てちこまです。
アプリを作成しているとユーザに特定の操作以外させたくない場面があると思います。
例えば、重要な入力やチュートリアル。同意画面などです。
今回はSearchView(EditText)と他のコンポーネントが同時に存在する画面で入力時に他の操作が出来ないようにフィルターをかけます。
目的
- 誤タップの防止
- 入力中であることを視覚的に見せる
完成画像
実装内容
今回はとにかく一番楽だろうとFrameLayoutでレイアウトの重ね合わせで実装しました。
最上層
中間層
真っ黒で全画面に引き伸ばした半透明のView(Filter)
最下層
中間のFilterを通常時は非表示にしてSearchViewにFocusが当たった時のみ表示することで完成画像のようなそれっぽい動きさせてます。
XML
※長いので一部省略しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!--3層目terminal_setting_list--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/setting_list_constraintLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/textColorDark" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.cardview.widget.CardView android:id="@+id/setting_list_container" android:layout_width="match_parent" android:layout_height="160dp" android:layout_marginStart="8dp" android:layout_marginTop="116dp" android:layout_marginEnd="8dp" app:cardBackgroundColor="@color/backgroundCard" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/current_setting_btn" android:layout_width="match_parent" android:layout_height="50dp" android:background="?android:attr/selectableItemBackground" android:clickable="true" android:focusable="true" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <View android:id="@+id/divider1" android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/darker_gray" app:layout_constraintBottom_toTopOf="@+id/pref_search_btn" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/current_setting_btn" /> <View android:id="@+id/pref_search_btn" android:layout_width="match_parent" android:layout_height="50dp" android:background="?android:attr/selectableItemBackground" android:clickable="true" android:focusable="true" app:layout_constraintBottom_toTopOf="@+id/address_search_btn" app:layout_constraintTop_toBottomOf="@+id/current_setting_btn" /> <View android:id="@+id/divider2" android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/darker_gray" app:layout_constraintBottom_toTopOf="@+id/address_search_btn" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/pref_search_btn" /> <View android:id="@+id/address_search_btn" android:layout_width="match_parent" android:layout_height="50dp" android:background="?android:attr/selectableItemBackground" android:clickable="true" android:focusable="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/divider2" /> <TextView android:id="@+id/current_setting_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:text="@string/currentPlaceSetting" android:textColor="@color/textColorLight" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="@+id/current_setting_btn" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/pref_search_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:text="@string/prefSearch" android:textColor="@color/textColorLight" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="@+id/pref_search_btn" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/pref_search_btn" /> <TextView android:id="@+id/address_search_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:text="@string/addressSearch" android:textColor="@color/textColorLight" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="@+id/address_search_btn" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/address_search_btn" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:id="@+id/recently_setting_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" app:cardBackgroundColor="@color/backgroundCard" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/setting_list_container"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/recently_list_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/recently_list_textView" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> </androidx.constraintlayout.widget.ConstraintLayout> <!--2層目操作制限Filter--> <View android:id="@+id/blackFilterView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/blackFilter" android:clickable="true" android:focusable="true" android:visibility="gone"/> <!--1層目keyword_search--> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:focusable="true"> <androidx.cardview.widget.CardView android:id="@+id/search_keyword_container" android:layout_width="match_parent" android:layout_height="100dp" android:layout_margin="8dp" app:cardBackgroundColor="@color/backgroundCard" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/search_keyword_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="@string/search_keyword" android:textColor="@color/textColorLight" android:textSize="20sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.appcompat.widget.SearchView android:id="@+id/terminal_searchView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:actionBarWidgetTheme="@android:style/Theme.Holo" android:background="@color/backgroundSearchView" app:defaultQueryHint="@string/search_hint" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/search_keyword_textView" app:queryHint="@string/search_hint"> </androidx.appcompat.widget.SearchView> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> </androidx.constraintlayout.widget.ConstraintLayout> </FrameLayout> |
FrameLayoutは書いたパーツが下から上に積み上がっていきます。またフィルターの半透明色の指定はこちらのQiita記事を参考に行ってください。今回は#7F000000を使用しています。
コード
※一部抜粋
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/** * SearchViewのFocus制御とFilterアニメーションの実装 */ private fun searchViewInit(){ terminal_searchView.setOnQueryTextFocusChangeListener() { v, hasFocus -> if(hasFocus){ filterIn() }else{ filterOut() } } } /** * 操作制限Filterの表示、非表示 */ private fun filterIn(){ blackFilterView.run{ setOnClickListener{ terminal_searchView.clearFocus() } visibility = View.VISIBLE val anim = AnimationUtils.loadAnimation(context,R.anim.fadein) postDelayed({startAnimation(anim)},0) } } private fun filterOut(){ blackFilterView.run{ visibility = View.GONE val anim = AnimationUtils.loadAnimation(context,R.anim.fadeout) postDelayed({startAnimation(anim)},0) isClickable = false } } |
今回はSearchViewにフォーカスが当たるとフィルターをフェードインし、フォーカスが外れるかフィルターをタップ(フィルタータップ時フォーカスが外れるようにしている)することでフィルターがフェードアウトするようにしています。
ここでの最重要ポイントはフィルターが消える際にisClickableをfalseにすることでフィルターのクリックリスナーを解除している点です。
これをしないとフィルターが解除されてからリスト、今回で言うところの「現在地を出発地とする」などをタップ出来なくなります。
ViewをremoveViewで消すことでも同じことが出来ますが再度表示する時にviewを再生成する手間がかかるのでこちらがベストかと思います。
まとめ
isClickableあたりの検証で1時間ほどかけてしまいました...
ゴリ押し感もありますがとりあえずこれで満足しています。
一応自力でフィルターつけたい時はこれでなんとかなるかな~。
参考記事
Viewに付与したOnClickListenerを解除する方法