简体   繁体   English

如何在 Android kotlin 中为 EditText 定义最小值和最大值?

[英]how to define a min and max value for EditText in Android kotlin?

我想制作一个积分计算器,我有两个 editText,其中一个是目标,一个是失败,总共必须是 40,我该怎么做?

MainActivity.kt主活动.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活动_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>

Let's try.咱们试试吧。 I hope you problem will be solved.我希望你的问题会得到解决。 Ask if you have any query.询问您是否有任何疑问。 Thanks谢谢

<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()
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在 Android 中为 EditText 定义最小值和最大值? - Is there a way to define a min and max value for EditText in Android? 我们如何为 android 中的 EditText 定义最小和最大自定义值之间的范围,例如 85 和 110 - How We define range between min and max custom value like 85 and 110 for EditText in android 有没有办法为 EditText 定义最小值和最大值。 EG 20 - 200(不是从 1 开始) - Is there a way to define a min and max value for a EditText. EG 20 - 200 (not starting at 1) 如何在Android的RangeSeekBar中获取当前的最小值和最大值? - How to get current min and max value in RangeSeekBar in android? 如何在 Android 中使用 mpchart 在折线图中显示最小值和最大值 - how to show min and max value in linechart using mpchart in Android 如何格式化android-range-seek-bar的最小值和最大值? - How to formats the min and max value of android-range-seek-bar ? 如何在 EditText 中设置最小文本(强制)和最大文本 - How to set Min text (Mandatory) and Max text in EditText 在Android Studio中获取最小值和最大值 - Get the min and max value in android studio 设置Int的最小值和最大值? Android的 - Set Min and Max Value for Int? Android Android - 获取接近传感器的最小值和最大值 - Android - get proximity sensor min and max value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM