清除edittext时出错的原因 [英] reason for error while clearing edittext

查看:82
本文介绍了清除edittext时出错的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个非常愚蠢的问题。我有一个代码,当我在edittext中输入一个值时,它将拆分检查某些条件的字符串。如果条件为假,我需要清除edittext。当我在edittext中输入错误的信息时,我会强制关闭错误。



  public   void  afterTextChanged(可编辑的s){
字符串 iGTIN = pscan.getText( )的ToString();
if (iGTIN.length()== 40 || iGTIN.contains( (01))&& iGTIN.contains( (21))){
String pGtin = iGTIN.substring ( 4 18 );
字符串 sSerialNo = iGTIN.substring( 22 32 );
gtin.setText(pGtin);
serial.setText(sSerialNo);
pscan.setText( );



}
else
{
error.setText(< span class =code-string>
不是有效的主扫描码。请再次检查并扫描);
pscan.setText( );
}

解决方案

这与如何使用子字符串拆分数字 [ ^ ],我已经回答了。如果要在输入内容时验证部分内容,则不应对子字符串的长度或其出现位置进行假设。在上述情况下,如果用户输入:

 foo(01)abc(21)bar 



your代码将崩溃,因为您试图提取超出输入数据限制的子字符串。


i have this very silly question. i have a code that when i enter a value in edittext it will split the string checking some condition. if the condition is false i need to clear the edittext. when i enter a wrong information into the edittext i get force close error.

public void afterTextChanged(Editable s) {
               String iGTIN = pscan.getText().toString();
               if (iGTIN.length() == 40 || iGTIN.contains("(01)") && iGTIN.contains("(21)")) {
                   String pGtin = iGTIN.substring(4, 18);
                   String sSerialNo = iGTIN.substring(22, 32);
                   gtin.setText(pGtin);
                   serial.setText(sSerialNo);
                   pscan.setText(" ");



               }
               else
               {
                   error.setText("Not a Valid Primary Scan Code..Please check and scan again");
                   pscan.setText("");
               }

解决方案

This is exactly the same issue as how to split numbers using substring[^], which I already answered. If you want to verify parts of the content as it is entered then you should not make assumptions about the length of substrings, or where they occur. In the above case if the user enters:

foo(01)abc(21)bar


your code will crash, because you are trying to extract a substring beyond the limit of the entered data.


这篇关于清除edittext时出错的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆