简体   繁体   English

如何在 Android Studio 的 EditText 中输入两个数字的条件

[英]how to set condition on two numbers entered in EditText in Android Studio

I have just started android studio, i am stuck on EditText that has to take two numbers as input from user.我刚刚开始 android 工作室,我被困在 EditText 上,它必须从用户那里输入两个数字。 Basically it is used for a 7-segment display with two digits that is made by views.基本上,它用于由视图生成的带有两位数字的 7 段显示器。 i am able to turn on the first digit when user enters the first number, but i don't know how to turn on the second digit when user enters second number in EditText.当用户输入第一个数字时,我可以打开第一个数字,但是当用户在 EditText 中输入第二个数字时,我不知道如何打开第二个数字。

below is the MainActivity.java code that i've tried.下面是我尝试过的 MainActivity.java 代码。

 public void afterTextChanged(Editable s) {
        }
        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }
        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
            String input = txtInput.getText().toString();

//below code is for when user enter 1, the 7-segment will turn 1 //下面的代码是当用户输入1时,7段将变为1

            if (txtInput.getText().toString().equals("1")) {
                view1.setBackgroundColor(Color.WHITE);
                view2.setBackgroundColor(Color.RED);
                view3.setBackgroundColor(Color.RED);
                view4.setBackgroundColor(Color.WHITE);
                view5.setBackgroundColor(Color.WHITE);
                view6.setBackgroundColor(Color.WHITE);
                view7.setBackgroundColor(Color.WHITE);
            }

the first digit turned red as the user entered 1, how to apply this on second digit aswell当用户输入 1 时,第一个数字变成红色,如何在第二个数字上应用这个

You used String input = txtInput.getText().toString();您使用String input = txtInput.getText().toString(); but in your code there's no var with this name "txtInput", I think you mistakes and you mean CharSequence s in onTextChanged method.但是在您的代码中没有这个名称为“txtInput”的变量,我认为您犯了错误,您的意思是onTextChanged方法中CharSequence s

However, if you wanna get the second number that is written in onTextChanged you can use the following但是,如果您想获得onTextChanged中写入的第二个数字,您可以使用以下内容

if( s != null && s.length >= 2) {

String secondNumber = s[1]

Log.d(TAG, "onTextChanged: $secondNumber") ==> this log to check for the number

}

then you can continue doing your if check然后你可以继续做你的 if 检查

 if (secondNumber.getText().equals("8")) {
    doSomeThing();
   }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM