Fix TextInputDialog keyboard behavior

This commit is contained in:
Koitharu
2020-04-19 08:06:24 +03:00
parent bc825681a8
commit ef4dd82e92
4 changed files with 17 additions and 17 deletions

View File

@@ -3,28 +3,16 @@ package org.koitharu.kotatsu.ui.common.dialog
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.text.InputFilter
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.dialog_input.view.* import kotlinx.android.synthetic.main.dialog_input.view.*
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.utils.ext.showKeyboard
class TextInputDialog private constructor(private val delegate: AlertDialog) : class TextInputDialog private constructor(private val delegate: AlertDialog) :
DialogInterface by delegate { DialogInterface by delegate {
init {
delegate.setOnShowListener {
val view = delegate.findViewById<TextView>(R.id.inputEdit)?:return@setOnShowListener
view.post {
view.requestFocus()
view.showKeyboard()
}
}
}
fun show() = delegate.show() fun show() = delegate.show()
class Builder(context: Context) { class Builder(context: Context) {
@@ -34,10 +22,6 @@ class TextInputDialog private constructor(private val delegate: AlertDialog) :
private val delegate = AlertDialog.Builder(context) private val delegate = AlertDialog.Builder(context)
.setView(view) .setView(view)
.setOnDismissListener {
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
fun setTitle(@StringRes titleResId: Int): Builder { fun setTitle(@StringRes titleResId: Int): Builder {
delegate.setTitle(titleResId) delegate.setTitle(titleResId)
@@ -54,6 +38,17 @@ class TextInputDialog private constructor(private val delegate: AlertDialog) :
return this return this
} }
fun setMaxLength(maxLength: Int, strict: Boolean): Builder {
with(view.inputLayout) {
counterMaxLength = maxLength
isCounterEnabled = maxLength > 0
}
if (strict && maxLength > 0) {
view.inputEdit.filters += InputFilter.LengthFilter(maxLength)
}
return this
}
fun setInputType(inputType: Int): Builder { fun setInputType(inputType: Int): Builder {
view.inputEdit.inputType = inputType view.inputEdit.inputType = inputType
return this return this

View File

@@ -86,6 +86,7 @@ class CategoriesActivity : BaseActivity(), OnRecyclerItemClickListener<Favourite
.setHint(R.string.enter_category_name) .setHint(R.string.enter_category_name)
.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) .setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)
.setNegativeButton(android.R.string.cancel) .setNegativeButton(android.R.string.cancel)
.setMaxLength(12, false)
.setPositiveButton(R.string.rename) { _, name -> .setPositiveButton(R.string.rename) { _, name ->
presenter.renameCategory(category.id, name) presenter.renameCategory(category.id, name)
}.create() }.create()
@@ -98,6 +99,7 @@ class CategoriesActivity : BaseActivity(), OnRecyclerItemClickListener<Favourite
.setHint(R.string.enter_category_name) .setHint(R.string.enter_category_name)
.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) .setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)
.setNegativeButton(android.R.string.cancel) .setNegativeButton(android.R.string.cancel)
.setMaxLength(12, false)
.setPositiveButton(R.string.add) { _, name -> .setPositiveButton(R.string.add) { _, name ->
presenter.createCategory(name) presenter.createCategory(name)
}.create() }.create()

View File

@@ -70,6 +70,7 @@ class FavouriteCategoriesDialog : BaseBottomSheet(R.layout.dialog_favorite_categ
TextInputDialog.Builder(context ?: return) TextInputDialog.Builder(context ?: return)
.setTitle(R.string.add_new_category) .setTitle(R.string.add_new_category)
.setHint(R.string.enter_category_name) .setHint(R.string.enter_category_name)
.setMaxLength(12, false)
.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) .setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)
.setNegativeButton(android.R.string.cancel) .setNegativeButton(android.R.string.cancel)
.setPositiveButton(R.string.add) { _, name -> .setPositiveButton(R.string.add) { _, name ->

View File

@@ -13,6 +13,7 @@
android:id="@+id/inputLayout" android:id="@+id/inputLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:boxBackgroundColor="@android:color/transparent"
app:boxBackgroundMode="filled"> app:boxBackgroundMode="filled">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
@@ -21,6 +22,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:imeOptions="actionDone" android:imeOptions="actionDone"
android:maxLines="1" android:maxLines="1"
android:singleLine="true"
tools:text="@tools:sample/lorem[2]" /> tools:text="@tools:sample/lorem[2]" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>