android如何在Edittext中检测多个字符串值

时间:2018-09-26 04:28:26

标签: android

如上所述。我想检测MultiAutoCompleteTextView中所有相似的文本。它只检测在EditText中检测到的第一个字符串。我想要的是,如果我有5对,它将检测所有的字符串。我想用它来创建文本验证器,例如:

<script> </script>
<script> </script>
<script> </script>
<script> </script>
<script> </script>

如果至少一个字符串不相似..它将显示一条敬酒消息以及多个检测到的相似文本

Toast.makeText(this, "Script Tag is Correct" + i, Toast.LENGTH_SHORT).show();

这是我尝试过的:

for (int i = 0 ; i <= 0; i ++ ) {

  String text = txtEditor.getText().toString();
  if (text.contains("<script>") && text.contains("</script>"))
  {
     // If Editor contains the specified String
      Toast.makeText(this, "Script Tag is Correct" + i, Toast.LENGTH_SHORT).show();
      if (text.contains("var") && text.contains("{") && text.contains("}") && text.contains(";")){
          Toast.makeText(this, "Var Tag is Correct", Toast.LENGTH_SHORT).show();
      }else{
          Toast.makeText(this, "Var Tag is inCorrect", Toast.LENGTH_SHORT).show();
      }
 }
 break;

编辑: 我想要的是如果我的EditText包含多个类似的字符串,例如下面。它会检测所有脚本标签,如果脚本标签拼写错误或缺少字符串,则会显示一条Toast消息。

 <script>
    var myObj = { name: "John", age: 31, city: "New York" };
    var myJSON = JSON.stringify(myObj);
    window.location = "demo_json.php?x=" + myJSON;
    </script>
    <script> var myObj = { name: "John", age: 31, city: "New York" }; 
     var myJSON = JSON.stringify(myObj); window.location = "demo_json.php?x=" + myJSON; </script>

2 个答案:

答案 0 :(得分:0)

//声明arrayList

ArrayList scriptStartTag = new ArrayList();

ArrayList scriptEndTag = new ArrayList();

for (int i = 0 ; i <= 0; i ++ ) {

              String text = txtEditor.getText().toString();
              if (text.contains("<script>"))
              {

                  // If Editor contains the specified String
                  Toast.makeText(this, "Script Tag is Correct" + i, Toast.LENGTH_SHORT).show();

                  //add in to start arrayList
                  scriptCount.add("startTagFound");
                  if (text.contains("<script>")){
                  //add in to end arrayList
                  scriptEndTag.add("endTagFound")
                  }



                  if (text.contains("var") && text.contains("{") && text.contains("}") && text.contains(";"))

                  {

                      Toast.makeText(this, "Var Tag is Correct", Toast.LENGTH_SHORT).show();
                  }
                  else

                  {

                      Toast.makeText(this, "Var Tag is inCorrect", Toast.LENGTH_SHORT).show();
                  }
             }
             }

//检查对是否为5,然后做您想做的事

 if (scriptStartTag.size() == 5 && scriptEndTag.size() == 5) {
            if (scriptStartTag.size() == scriptEndTag.size()) {
                //do what you want 
            } else {
                //your error msg 
            }
        } else {
            //your error msg 
        }

答案 1 :(得分:0)

我发现此代码对我有用。它从整个edittext中查找所有<script> </script>标签。循环内是否可能有for循环?并从我得到的索引中获取行号。

String text2 = txtEditor.getText().toString();
                    String word = "<script>";
                    String word2 = "</script>";
                    if (text2.contains(word) && text2.contains(word2)) {



                        //Start 1st if
                        for (int i = -1; (i = text2.indexOf(word, i + 1)) != -1; i++) {
                            Toast.makeText(this, "<Script> Tag is Correct " + i, Toast.LENGTH_SHORT).show();

                        }
                        for (int i = -1; (i = text2.indexOf(word2, i + 1)) != -1; i++) {
                            Toast.makeText(this, "</Script> Tag is Correct " + i, Toast.LENGTH_SHORT).show();



                        }


                    }

                        else
                        {

                            Toast.makeText(this, "Script Tag is inCorrect" , Toast.LENGTH_SHORT).show();
                        }