简体   繁体   English

每次点击后检查EditText值

[英]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) 每当用户执行一个动作时,我希望我的Activity检查TextView / EditText中String(Int)的值(inrease或减少我的文本字段中的值)

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. 从两个不同的Activite调用Activity,Activity A的起始值为20,Activity B的起始值为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 现在我希望用户更正此值(如果他喜欢)并且我需要检查,用户(A)的值不会低于20且用户(B)的值不会低于10,另一方面,当用户增加这个数字时,我需要打印警告文本+信息文本,如:W:“只有20(A)/ 10(B)的空间”我:“增加会带给你......”在这里,我将在我的字段中为给定的数字添加一些值

The XML: 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 在这里我的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. 绝对有效:if(t <20){...} - 好的。 But after every "onClick" the value due if/else seems a little bit long here in terms of Code. 但是在每次“onClick”之后,if / else的值在代码方面似乎有点长。 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);
    });

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

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