简体   繁体   中英

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. Basically it is used for a 7-segment display with two digits that is made by views. 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.

below is the MainActivity.java code that i've tried.

 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

            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

You used 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.

However, if you wanna get the second number that is written in onTextChanged you can use the following

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 (secondNumber.getText().equals("8")) {
    doSomeThing();
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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