退格键不会删除webview的Html文本输入中的泰语元音

时间:2016-11-06 06:56:21

标签: android webview backspace thai

我使用的是Android Studio 2.1.1。

我在webview中打开一个带有文本类型输入的本地html文件。我发现动态键盘的退格键不会删除任何在辅音字母之上或之下的泰语元音。但是,在EditText视图中,退格键与Thai元音没有任何问题。它删除所有字符,包括泰语辅音和元音。

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText simpleEditText = (EditText) findViewById(R.id.simpleEditText);
    WebView wv = (WebView)findViewById(R.id.webview);
    wv.loadUrl("file:///android_asset/test_input.html");
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.thongjoon.edittext.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@id/simpleText"
    android:text="Hello World!"/>

<EditText
    android:id="@+id/simpleEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/simpleText"
    android:hint="Enter Your Name Here" />
<WebView android:id="@+id/webview"
         android:layout_below="@id/simpleEditText"
         android:layout_width="fill_parent"
         android:layout_height="50dp"/>

</RelativeLayout>

test_input.html

<!doctype html>
<html lang="th">
   <head>
   <meta charset="utf-8">
   <title>Document</title>
   </head>
<body>
  ทดสอบ<input type="text" name="yourname" value="Your Name"><br>
</body>
</html>

我是Android新手。我真的不知道如何解决这个问题。请指教。

1 个答案:

答案 0 :(得分:0)

我决定不使用类型文本的html输入。相反,我创建了一个JavaScriptInterface类来将变量传递给javascript函数。该变量来自EditText视图。我点击按钮传递它。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.thongjoon.edittext.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@id/simpleText"
        android:text="Hello World!"/>

    <EditText
        android:id="@+id/simpleEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/simpleText"
        android:singleLine="true"
        android:maxLength="20"
        android:hint="Enter Text Here" />
    <Button
        android:id="@+id/btnSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/simpleEditText"
        android:text="Go"/>

    <WebView android:id="@+id/webview"
         android:layout_below="@+id/btnSearch"
         android:layout_width="fill_parent"
         android:layout_height="150dp"/>

</RelativeLayout>

test_input.html

<!doctype html>
<html lang="th">
<head>
  <meta charset="utf-8">
  <title>Document</title>

</head>
<body>

  ทดสอบการส่งตัวแปรจาก Android<br>
  Passing a variable from Android to Javascript<br>

  <script>
    function init(val){
      alert(val);
    }
  </script>
</body>

JavaScriptInterface.java

import android.content.Context;
import android.webkit.JavascriptInterface;

public class JavaScriptInterface {
    Context mContext;
    JavaScriptInterface(Context c) {
        mContext = c;
    }
    @JavascriptInterface
    public static String getFromAndroid(String myVar) {
        return myVar;
    }
}

MainActivity.java

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    String inputText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // hide virtual keyboard. Show keyboard when user touches EditText view
        getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
        );

        setContentView(R.layout.activity_main);

        final EditText simpleEditText = (EditText) findViewById(R.id.simpleEditText);
        final Button btn_search =(Button) findViewById(R.id.btnSearch);

        final WebView webView = (WebView)findViewById(R.id.webview);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        //for showing Javascript alert box
        webView.setWebChromeClient(new WebChromeClient() {});
        webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        webView.loadUrl("file:///android_asset/test_input.html");

        btn_search.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //hide virtual keyboard
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                       inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            inputText = simpleEditText.getText().toString();
            webView.loadUrl("javascript:init('" + inputText + "')");
            }
        });
    }
}

非常感谢以下链接;

Passing data from java class to Web View html

Get Value of a Edit Text field How to pass a value/variable from an android java class to javascript/.html file in android 4.4.2 kitkat

Passing data from java class to Web View html