如何在edittext中禁用零后输入数字?

时间:2018-02-07 07:13:52

标签: java android android-layout android-edittext

如何在edittext中禁用零后输入的数字?

我已经验证了这个..

<mvc:annotation-driven  />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="mappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
    <property name="messageConverters">  
        <list>  
               <ref bean="mappingJacksonHttpMessageConverter" />  
        </list>  
    </property>  
</bean>  

<bean id="mappingJacksonHttpMessageConverter"  
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
     <property name = "supportedMediaTypes">  
          <list>  
            <bean class="org.springframework.http.MediaType">  
             <constructor-arg index="0" value="text"/>  
             <constructor-arg index="1" value="plain"/>  
             <constructor-arg index="2" value="UTF-8"/>  
            </bean>  
            <bean class="org.springframework.http.MediaType">  
             <constructor-arg index="0" value="*"/>  
             <constructor-arg index="1" value="*"/>  
             <constructor-arg index="2" value="UTF-8"/>  
            </bean>  
            <bean class="org.springframework.http.MediaType">  
             <constructor-arg index="0" value="text"/>  
             <constructor-arg index="1" value="*"/>  
             <constructor-arg index="2" value="UTF-8"/>  
            </bean>  
             <bean class="org.springframework.http.MediaType">  
             <constructor-arg index="0" value="application"/>  
             <constructor-arg index="1" value="json"/>  
             <constructor-arg index="2" value="UTF-8"/>  
            </bean>  
          </list>  
    </property>  
</bean>   

但如果用户输入01或001或02,03等,?即零后的数字? - &GT;我想限制这些数字

如何处理此类案件?

5 个答案:

答案 0 :(得分:1)

我认为你想限制那些以0开头的数字。为此,你必须使用.startsWith()

if (TextUtils.isEmpty(strBalance)) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT)
                        .show();
            } else if (strBalance.startsWith("0")) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be Zero", Toast.LENGTH_SHORT)
                        .show();
            }

答案 1 :(得分:1)

if (TextUtils.isEmpty(strBalance)) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be empty", Toast.LENGTH_SHORT)
                        .show();
            } else if (strBalance.equalsIgnoreCase("0")) {
                Toast.makeText(TransactionSellINActivity.this, "Amount should not be Zero", Toast.LENGTH_SHORT)
                        .show();
            }
            else if(strBalance.indexOf("0")!=a.length()-1)
                {
                Toast.makeText(TransactionSellINActivity.this, "Amount Not Valid", Toast.LENGTH_SHORT)
                        .show();
                }
                else{
                    Toast.makeText(TransactionSellINActivity.this, "Amount  Valid", Toast.LENGTH_SHORT)
                        .show();
                }

答案 2 :(得分:0)

private EditText  mEt;//your edittext
private TextWatcher mMoneyWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (!s.toString.isEmpty()) {
            mPayEtValue.removeTextChangedListener(mMoneyWatcher);
            if (s.toString.charAt(0)=='0') {
              mEt.setText(s.toString.substring(1,s.length()));  
            }

            mEt.addTextChangedListener(mMoneyWatcher);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
mEt.addTextChangedListener(mMoneyWatcher);

答案 3 :(得分:0)

你也可以这样做,让用户尽可能多地输入零。在正则表达式之后,将从字符串中删除前导零,之后您可以继续使用字符串。

if (!TextUtils.isEmpty(strBalance) && Integer.parseInt(strBalance) > 0) 
{
        strBalance=strBalance.replaceFirst("^0+(?!$)", "")       
}

答案 4 :(得分:0)

使用此代码后,用户无法在零后添加数字TRY IT

Edittext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {

            if (Edittext.getText().toString().startsWith("0")
                    && Edittext.getText().length() > 1) {

                Edittext.setText("0");
                Edittext.setSelection(Edittext.getText().toString().length());
            }
        }
    });

对你有用!!