简体   繁体   中英

Check EditText Value after every Click

I want my Activity to check the value of the String (Int) in my TextView / EditText everytime the user performs an action (inrease or decrease the value in my text field)

The Activity gets called from two different Activites, Activity A has a starting value of 20 and Activity B has a starting value of 10.

Now I want the user to correct this value (if he likes) and I need to check, that the value for User(A) won't get under 20 and the Value of User(B) won't get under 10, on the other side I need to print a warning text + information text when the User increases this number like: W: "There is only space for 20(A) / 10(B)" I: "Increasing will bring you ..." and here I'm going to put some values for the given number in my field

The XML:

<RelativeLayout
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:padding="@dimen/big_padding">

    <Button
        android:id="@id/btn_minus"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="-" />


    <Button
        android:id="@+id/btn_plus"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/number"
        android:layout_width="wrap_content"
        android:text="+" />


    <EditText
        android:id="@+id/number"
        android:inputType="number"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/btn_minus"
        android:layout_width="100dp" />

    <TextView
        android:id="@+id/info"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

    <TextView
        android:id="@+id/warning"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

</RelativeLayout>

and here my JavaCode

    EditText num;
    Button btn_plus, btn_minus;
    (..)
    if(id.equals("A")) {
          // Activity A, limit = 20
          num.setText("20");
          btn_plus.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  int t = Integer.parseInt(counterTxt.getText().toString());  
                  num.setText(String.valueOf(t+1)); 

              }
          }
          //same for btn_minus
    } else {
        // Activity B, limit=10
        num.setText("10");
    }

What definitely is working: if(t<20) {...} - OK. But after every "onClick" the value due if/else seems a little bit long here in terms of Code. Is there any more elegant way to solve this?

You are probably looking for something similar to this... This code will allow you to increment a value and watch the value change with each click.

    int number = 15;
    Button plus, minus;
    EditText value;

    plus.setOnClickListener(new View.onClickListener(){
        number++;

        if(number > max)
            number = max;

        value.setText(number);
    });


    minus.setOnClickListener(new View.onClickListener(){
        number--;

        if(number < min)
        number = min;

        value.setText(number);
    });

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