【问题标题】:limiting values of edittext to certain values in android将edittext的值限制为android中的某些值
【发布时间】:2017-05-04 13:18:51
【问题描述】:

我在我的代码中使用编辑文本。我需要将权重值限制为 160。如何实现这一点。

这是我正在使用的 XML

   android:id="@+id/txtWeight"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:inputType="number"
                        android:layout_weight="0"
                        android:background="@drawable/border"
                        android:padding="5dp"
                        android:maxLength="3"
                        android:textSize="15sp"
                        android:maxLines="1"
                        android:hint=""

我已将 maxLenght 设为“3”。所以用户可以选择输入 999 。我需要限制为 160。

【问题讨论】:

    标签: android xml android-textwatcher


    【解决方案1】:

    这样做

     InputFilter if = new InputFilter[]{ new InputFilterMinMax("0", "180")}
    

    或自定义

    InputFilter filterTwoDecimal = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    
                return null;
    
            }
        };
    

    【讨论】:

      【解决方案2】:

      试试这个:

      public class InputFilterMinMax implements InputFilter {
      
      private int min, max;
      
      public InputFilterMinMax(int min, int max) {
          this.min = min;
          this.max = max;
      }
      
      public InputFilterMinMax(String min, String max) {
          this.min = Integer.parseInt(min);
          this.max = Integer.parseInt(max);
      }
      
      @Override
      public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {   
          try {
              int input = Integer.parseInt(dest.toString() + source.toString());
              if (isInRange(min, max, input))
                  return null;
          } catch (NumberFormatException nfe) { }     
          return "";
      }
      
      private boolean isInRange(int a, int b, int c) {
          return b > a ? c >= a && c <= b : c >= b && c <= a;
      }
      }
      

      然后在您的片段/活动上:

      EditText et = (EditText) findViewById(R.id.myEditText);
      et.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "180")});
      

      【讨论】:

      • 谢谢!如果成功请标记为正确答案!
      【解决方案3】:

      您可以将 TextWatcher 添加到您的 EditText 并对其进行更改,这样它就不会允许不需要的值

      editText.addTextChangedListener(new TextWatcher() {
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
      
              // TODO Auto-generated method stub
          }
      
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
              // TODO Auto-generated method stub
          }
      
          @Override
          public void afterTextChanged(Editable s) {
      
              // TODO Auto-generated method stub
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2011-08-10
        • 1970-01-01
        • 2011-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-27
        • 1970-01-01
        • 2013-06-04
        相关资源
        最近更新 更多