Как определить минимальное и максимальное значение для EditText в Android kotlin? - PullRequest
0 голосов
/ 16 марта 2020

Я хочу сделать точечный калькулятор. У меня есть два editText, один из них - «Цели», а один - «Сбои». Всего их должно быть 40, как я могу это сделать?

Ответы [ 2 ]

2 голосов
/ 16 марта 2020

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>

Давайте попробуем. Я надеюсь, что ваша проблема будет решена. Спросите, есть ли у вас запрос. Спасибо

1 голос
/ 16 марта 2020
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
xmlns:android="http://schemas.android.com/apk/res/android" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/goal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Goal"
            android:textSize="40dp" />

        <EditText
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:text="0"
            android:inputType="number" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/fail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Fail"
            android:textSize="40dp" />

        <EditText
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:text="0"
            android:inputType="number" />
    </LinearLayout>


</LinearLayout>

<Button
    android:id="@+id/Enter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Enter" />

import android.os.Bundle
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var a=0
        var b=0
        if (text1.text!=null)
         a= Integer.parseInt(text1.getText().toString())
        if (text2.text!=null)
         b= Integer.parseInt(text2.getText().toString())
        Enter.setOnClickListener {
            if ((a + b) <= 40)
                Toast.makeText(this, "OK", Toast.LENGTH_LONG).show()
            else Toast.makeText(
                this,
                "Sum of goals and fails should be maximum 40",
                Toast.LENGTH_LONG
            ).show()
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...