Hide keyboard when clicked outside of name input field.

This commit is contained in:
2020-08-30 20:39:38 +02:00
parent 587651d697
commit f5cd5775cf
3 changed files with 40 additions and 17 deletions

View File

@@ -7,6 +7,7 @@ import android.text.InputType
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.WindowManager import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.ScrollView import android.widget.ScrollView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
@@ -43,6 +44,18 @@ class MainActivity : AppCompatActivity() {
updateView() updateView()
inputTeamA.setOnFocusChangeListener { view, b ->
if (b) {
hideKeyboard()
}
}
inputTeamB.setOnFocusChangeListener { view, b ->
if (b) {
hideKeyboard()
}
}
inputTeamA.doOnTextChanged { text, start, count, after -> inputTeamA.doOnTextChanged { text, start, count, after ->
if (inputTeamA.isFocused) { if (inputTeamA.isFocused) {
if (inputTeamA.text.isNotEmpty()) { if (inputTeamA.text.isNotEmpty()) {
@@ -102,6 +115,8 @@ class MainActivity : AppCompatActivity() {
} }
buttonAdd100.setOnClickListener { buttonAdd100.setOnClickListener {
giveFocusToAIfNone()
if (inputTeamA.isFocused) { if (inputTeamA.isFocused) {
currentRound.scoreA = try { currentRound.scoreA = try {
@@ -132,6 +147,8 @@ class MainActivity : AppCompatActivity() {
} }
buttonSub100.setOnClickListener { buttonSub100.setOnClickListener {
giveFocusToAIfNone()
if (inputTeamA.isFocused) { if (inputTeamA.isFocused) {
currentRound.scoreA = try { currentRound.scoreA = try {
inputTeamA.text.toString().toInt() - 100 inputTeamA.text.toString().toInt() - 100
@@ -329,6 +346,12 @@ class MainActivity : AppCompatActivity() {
} }
} }
private fun hideKeyboard() {
val imm: InputMethodManager =
getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
private fun giveFocusToAIfNone() { private fun giveFocusToAIfNone() {
if (!inputTeamA.isFocused && !inputTeamB.isFocused) { if (!inputTeamA.isFocused && !inputTeamB.isFocused) {
inputTeamA.requestFocus() inputTeamA.requestFocus()
@@ -336,10 +359,8 @@ class MainActivity : AppCompatActivity() {
} }
private fun undoLastRound() { private fun undoLastRound() {
history.revertLastRound() history.revertLastRound()
updateView() updateView()
} }
private fun updateView() { private fun updateView() {

View File

@@ -17,13 +17,13 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<LinearLayout <LinearLayout
android:id="@+id/Names" android:id="@+id/viewNames"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
<EditText <EditText
android:id="@+id/NameTeamA" android:id="@+id/nameTeamA"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@@ -37,7 +37,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Large" /> android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText <EditText
android:id="@+id/NameTeamB" android:id="@+id/nameTeamB"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@@ -53,7 +53,7 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/Score" android:id="@+id/viewScore"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">
@@ -86,7 +86,8 @@
android:id="@+id/scrollViewHistory" android:id="@+id/scrollViewHistory"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginTop="16dp"> android:layout_marginTop="16dp"
android:clickable="true">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -123,7 +124,7 @@
app:layout_constraintStart_toEndOf="@+id/left"> app:layout_constraintStart_toEndOf="@+id/left">
<LinearLayout <LinearLayout
android:id="@+id/Input" android:id="@+id/viewInput"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> android:orientation="horizontal">

View File

@@ -7,14 +7,14 @@
app:layout_behavior="@string/appbar_scrolling_view_behavior"> app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout <LinearLayout
android:id="@+id/Names" android:id="@+id/viewNames"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:orientation="horizontal" android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<EditText <EditText
android:id="@+id/NameTeamA" android:id="@+id/nameTeamA"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@@ -28,7 +28,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Large" /> android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText <EditText
android:id="@+id/NameTeamB" android:id="@+id/nameTeamB"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
@@ -44,11 +44,11 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/Score" android:id="@+id/viewScore"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/Names"> app:layout_constraintTop_toBottomOf="@+id/viewNames">
<TextView <TextView
android:id="@+id/scoreA" android:id="@+id/scoreA"
@@ -86,8 +86,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/Input" android:clickable="true"
app:layout_constraintTop_toBottomOf="@+id/Score"> app:layout_constraintBottom_toTopOf="@+id/viewInput"
app:layout_constraintTop_toBottomOf="@+id/viewScore">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -118,10 +119,10 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="1dp" android:layout_height="1dp"
android:background="?android:attr/listDivider" android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="@+id/Score" /> app:layout_constraintBottom_toBottomOf="@+id/viewScore" />
<LinearLayout <LinearLayout
android:id="@+id/Input" android:id="@+id/viewInput"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:orientation="horizontal" android:orientation="horizontal"