zoukankan      html  css  js  c++  java
  • Android中获取网页表单中的数据实现思路及代码

    在Android中获取网页里表单中的数据具体实现代码如下,感兴趣的各位可以参考过下哈,希望对大家有所帮助
    MainActivity如下:
    复制代码 代码如下:

    package cn.testjavascript;
    import java.util.StringTokenizer;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.app.Activity;
    /**
    * Demo描述:
    * 在Android中获取网页里表单中的数据
    */
    public class MainActivity extends Activity {
    private WebView mWebView;
    private String date =null;
    private String email = null;
    private String username = null;
    private String sex = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();
    }
    private void init(){
    mWebView=(WebView) findViewById(R.id.webView);
    initWebViewSettings();
    mWebView.loadUrl("file:///android_asset/form.html");
    //注意addJavascriptInterface方法中第二参数
    //它表示我们的java对象javaClass的别名.
    //这样Javascript就可以通过该别名来调用Android中的方法
    //即Javascript代码中的:window.testform.send(date+"|"+email+"|"+name+"|"+sex);
    //send是方法名
    //testform是别名
    mWebView.addJavascriptInterface(new Object() {
    public void send(String userInfo) {
    StringTokenizer userInfoStringTokenizer = new StringTokenizer(userInfo, "|");
    date = userInfoStringTokenizer.nextToken();
    email = userInfoStringTokenizer.nextToken();
    username = userInfoStringTokenizer.nextToken();
    sex = userInfoStringTokenizer.nextToken();
    System.out.println("userInfoStringTokenizer="+userInfoStringTokenizer.toString());
    System.out.println("date=" + date);
    System.out.println("email=" + email);
    System.out.println("username=" + username);
    System.out.println("sex=" + sex);
    };
    }, "testform");

    }
    private void initWebViewSettings(){
    mWebView.setVerticalScrollBarEnabled(false);
    mWebView.setHorizontalScrollBarEnabled(false);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.requestFocus();
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    }

    }

    main.xml如下:
    复制代码 代码如下:

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    />
    </RelativeLayout>

    form.html如下:
    复制代码 代码如下:

    <body>
    <form action="" method="post">
    时间:<br>
    <select id="shijian" name="date">
    <option value="2011">2011</option>
    <option value="2012">2012</option>
    <option value="2013">2013</option>
    <option value="2014">2014</option>
    <option value="2015">2015</option>
    </select><br>
    邮箱:
    <input id="email" type="text" name="emailID" />
    <br>
    昵称:
    <input id="name" type="text" name="username" />
    <br>
    性别:<br>
    <input id="men" type="radio" name="sex" value="men"/>男
    <input id="women" type="radio" name="sex" value="women"/>女
    <br>
    <input type="submit" value="注册" onclick="f()"/>
    <input type="button" value="取消" />
    </form>
    </body>
    <script type="text/JavaScript" language="javascript">
    function f(){
    var email = document.getElementById('email').value;
    var name = document.getElementById('name').value;
    var date = document.getElementById('shijian').value;
    if(document.getElementById('men').checked && !document.getElementById('women').checked){
    var sex = document.getElementById('men').value;
    }else if(!document.getElementById('men').checked && document.getElementById('women').checked){
    var sex = document.getElementById('women').value;
    }
    window.testform.send(date+"|"+email+"|"+name+"|"+sex);
    }
    </script>
  • 相关阅读:
    Android Material Design控件使用(2)——FloatButton TextInputEditText TextInputLayout 按钮和输入框
    HbuilderX 常用快捷键
    Android Material Design控件使用(一)——ConstraintLayout 约束布局
    git 命令行 修改文件 并push(阿里云)
    【http转https】其之二:申请Let's Encrypt颁发SSL证书
    git: fatal: Could not read from remote repository
    Android Vector曲折的兼容之路
    Gradle Build速度加快终极方法(android studio)
    android studio 更新 Gradle错误解决方法(Gradle sync failed)
    开发Android必知的工具
  • 原文地址:https://www.cnblogs.com/zhwl/p/3178171.html
Copyright © 2011-2022 走看看