MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main2.*
import kotlin.math.log
class MainActivity : AppCompatActivity() {
val total: Int = 40 //Max Limit
var first : Int = 0 //for first edit text GOALS
var second : Int = 0 // For Fails
var previous : Int = 0 //store Previous value for if user enter exceed limit
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
et1.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
if(s.toString().equals("")){
first = 0
}
else {
first = et1.text.trim().toString().toInt()
}
val temp : Int = (second + first)
if(temp>40){
et1.setText(Integer.toString(previous))
first = previous
Log.v("ABCD", "MAX Limit is 40")
} else {
Log.v("ABCD", first.toString())
}
}
override fun beforeTextChanged(s: CharSequence, start: Int,
count: Int, after: Int) {
if(et1.text.trim().toString().equals("")){
previous = 0
} else {
previous = et1.text.trim().toString().toInt()
}
}
override fun onTextChanged(s: CharSequence, start: Int,
before: Int, count: Int) {
}
})
et2.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
if(s.toString().equals("")){
second = 0
}
else {
second = et2.text.trim().toString().toInt()
}
val temp : Int = (second + first)
if(temp>40){
et2.setText(Integer.toString(previous))
second = previous
Log.v("ABCD", "MAX Limit is 40")
} else {
//et2.setText(Integer.toString(second))
Log.v("ABCD", second.toString())
}
}
override fun beforeTextChanged(s: CharSequence, start: Int,
count: Int, after: Int) {
if(et2.text.trim().toString().equals("")){
previous = 0
} else {
previous = et2.text.trim().toString().toInt()
}
}
override fun onTextChanged(s: CharSequence, start: Int,
before: Int, count: Int) {
}
})
}
}
activity_main. xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</LinearLayout>
Давайте попробуем. Я надеюсь, что ваша проблема будет решена. Спросите, есть ли у вас запрос. Спасибо