如何获取在android WebView页面的EditText中输入的值

时间:2016-08-24 11:21:01

标签: android android-layout android-webview

我正在开发一个应用程序,我想在android WebView的网页中获取用户输入的值。我们如何获取在网页的EditText中输入的数据 感谢

1 个答案:

答案 0 :(得分:0)

这是java代码:

wv_load.addJavascriptInterface(new Object()
            {
                @JavascriptInterface
                public void performClick(String strName,String strAge,String strEmail)
                {
                    Toast.makeText(WebActivity.this, "Input Provided "+strName+" "+strAge+" "+strEmail, Toast.LENGTH_LONG).show();
                }
            }, "valid");

和带有javascript代码的HTML:

<html>
<head>

    <script language="javascript">

   var at;
   var age;
   var name;

   function validClick()
   {
      at = document.getElementById("email").value;
      age = document.getElementById("age").value;
      name = document.getElementById("name").value;

      document.getElementById('lbl').innerHTML = '';
      document.getElementById('lbl2').innerHTML = '';
      document.getElementById('lbl3').innerHTML = '';

      var ret=myValidation();

      if(ret==true){

      valid.performClick(name,age,at);

      document.getElementById('lbl').innerHTML = '';
      document.getElementById("demo_form").reset();

      }else{

      }

   }

   function myValidation() {

    submitOK = "true";

    if (!validateName(name)) {
        document.getElementById('lbl').innerHTML = 'Enter Full Name!';
        submitOK = "false";
    }

    if (isNaN(age) || age < 1 || age > 100) {
        document.getElementById('lbl2').innerHTML = 'Enter Valid Age!';
        submitOK = "false";
    }

    if (!validateEmail(at)) {
        document.getElementById('lbl3').innerHTML = 'Enter Valid Email!';
        submitOK = "false";
    }

    if (submitOK == "false") {

        return false;
    }
    else{

        return true;

    }
    }

    function validateName(name){
    var reName=/^(([A-Za-z]+[\-\']?)*([A-Za-z]+)?\s)+([A-Za-z]+[\-\']?)*([A-Za-z]+)?$/;
    return reName.test(name);
    }

    function validateEmail(at) {
       var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
       return re.test(at);
    }

    </script>

</head>
<body>
<form name="demo_form" id="demo_form">
    Name : <input type="text" id="name" style="margin-left: 16px;" size="20">
    <label id="lbl" style="color:red"></label><br>
    Age : <input type="text" id="age" style="margin-left: 30px;" size="20">
    <label id="lbl2" style="color:red"></label><br>
    Email: <input type="text" id="email" style="margin-left: 22px;" size="20">
    <label id="lbl3" style="color:red"></label><br><br>

    <div>
        <button type="button" id="ok" style="font-weight: 700; margin-left: 70px;"
                onclick="validClick();">Submit
        </button>
    </div>
</form>
</body>
</html>