简体   繁体   English

Android WebView - 以编程方式在第 3 方网页的输入字段上强制使用数字键盘?

[英]Android WebView - Programmatically forcing numeric keyboard on 3rd party webpage's input field?

Quick context: We have a pretty unique situation where we're switching to a new HR platform which will only allow our factory employees to timekeep via a timeclock webpage.快速上下文:我们有一个非常独特的情况,我们正在切换到一个新的人力资源平台,该平台将只允许我们的工厂员工通过时钟网页计时。 We already have several Samsung Galaxy Tab A devices positioned throughout our building that serve as timeclock kiosks for the current HR system.我们已经在我们的大楼各处放置了几台三星 Galaxy Tab A 设备,作为当前 HR 系统的时钟亭。 Since we can only lock these tablets down to a single app at a time via Knox, a Chrome web browser for example would let employees accidentally navigate off the new platform's timeclock webpage and create issues for others trying to clock-in or out.由于我们只能通过 Knox 一次将这些平板电脑锁定到一个应用程序,例如 Chrome web 浏览器会让员工意外地离开新平台的时钟网页,并给其他尝试打卡或打卡的人带来问题。

SO , we already setup an Android app that implements a basic WebView hard-coded to the timeclock URL the new HR company provided us.所以,我们已经设置了一个Android应用程序,它实现了一个基本的 WebView 硬编码到新人力资源公司提供给我们的时钟 URL。 HOWEVER , the big problem here is that there's no on-page keyboard and the badge ID field is of type text , which invokes the Android qwerty keyboard for a badge ID PIN that would only ever be all numbers... A 3rd-party company built this webpage for the HR company so I'd be surprised if we could ever get it changed on their end.但是,这里的大问题是没有页面键盘,并且徽章 ID 字段的类型为文本,它调用 Android qwerty 键盘以获得徽章 ID PIN,而徽章 ID PIN 只能是所有数字......第 3 方公司为人力资源公司建立了这个网页,所以如果我们能最终改变它,我会感到惊讶。 我们的带有 WebView 的自定义 Android 应用程序与同一站点上的开发工具面板并排仿真。


Just to show some of our basic boilerplate to achieve the above...只是为了展示我们实现上述目标的一些基本样板......

AndroidManifest.xml AndroidManifest.xml

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Paytime Time Clock"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.PaytimeWebpage"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

MainActivity.java MainActivity.java

    public class MainActivity extends AppCompatActivity {
        private WebView webView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            webView = (WebView) findViewById(R.id.webview);
            webView.setWebViewClient(new WebViewClient());
            webView.setInitialScale(300);
            webView.loadUrl("https://site.time.clock");

            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);

        }
    }

Activity_Main.xml Activity_Main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </RelativeLayout>

We've done tons of exhaustive searching and really tried to think outside the box here but as only 2 SysAdmins at a small manufacturer we are by no means seasoned Java devs: We've tried researching and attempting the following ideas:我们进行了大量详尽的搜索,并真正尝试跳出框框思考,但作为一家小型制造商的 2 名系统管理员,我们绝不是经验丰富的 Java 开发人员:我们尝试研究并尝试以下想法:

  • Event listener for whenever the qwerty keyboard is invoked to then instead invoke the numeric or telephone keyboard每当调用 qwerty 键盘然后调用数字或电话键盘时的事件侦听器
  • Get HTML content from WebView, look for the <input type="text"> tag and try to programmatically change it to either number or tel in order to get the right keyboard to show从 WebView 获取 HTML 内容,查找<input type="text">标签并尝试以编程方式将其更改为numbertel ,以便获得正确的键盘显示
  • Force our app to invoke the numeric keyboard and strictly keep it there above the web page强制我们的应用程序调用数字键盘并将其严格保留在 web 页面上方
  • We've seen guys mentioning the EditText class for configuring input and keyboard types, but the problem with that is of course that we're not handling the input in question here within our app, it's on someone else's webpage through our WebView我们已经看到有人提到EditText class 用于配置输入和键盘类型,但问题当然是我们没有在我们的应用程序中处理这里有问题的输入,它通过我们的 WebView 在其他人的网页上

This is of course a very minor edge-case issue that we're forced to come up with a complicated solution to ourselves... We can observe which input types yield which keyboards on any Android device.这当然是一个非常小的边缘案例问题,我们被迫为自己想出一个复杂的解决方案……我们可以观察在任何 Android 设备上哪些输入类型产生哪些键盘 All we have to do is get a numeric soft-keyboard shown on this WebView'd site within our app such that employees can easily walk up, input their badge and complete their clock action.我们所要做的就是在我们的应用程序中的这个 WebView 站点上显示一个数字软键盘,这样员工就可以轻松地走上去,输入他们的徽章并完成他们的时钟操作。

I hope this was concise but thorough... There must be a creative solution here, does anybody have any suggestions or recommendations of how we can accomplish this?我希望这是简洁但彻底的...这里必须有一个创造性的解决方案,有人对我们如何实现这一目标有任何建议或建议吗? Even just pointing us in the right direction would be hugely appreciated... THANKS!!即使只是为我们指明正确的方向,我们也将不胜感激……谢谢!

In your WebViewClient, override onPageFinished() and do the following in it:在您的 WebViewClient 中,覆盖onPageFinished()并在其中执行以下操作:

webView.loadUrl("javascript:document.getElementById('FldBadge').getElementsByTagName('input')[0].type='number';");

The idea is to modify the loaded page so that it behaves the way you need.这个想法是修改加载的页面,使其按照您需要的方式运行。

UPDATE: Full code更新:完整代码

        public class MyWebViewClient extends WebViewClient {
          public void onPageFinished (WebView view, String url) {
            if(url.equals("https://my.website.url")) {     
webView.loadUrl("javascript:document.getElementById('FldBadge').getElementByTagName('input')[0].type='number';");
            }
          }
        }

Don't forget to change the URL in the code to your own.别忘了把代码中的URL改成你自己的。

In the activity's onCreate:在活动的 onCreate 中:

webview.setWebViewClient(new MyWebViewClient());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM