2016-11-06 65 views
0

我正在使用Android Studio 2.1.1。Backspace鍵不會刪除webview的Html文本輸入上的泰語元音

我打開一個本地html文件,在我的webview中輸入文本類型。我發現動態鍵盤的退格鍵不會刪除任何超過或低於輔音字母的泰語元音。但是,在EditText視圖中,退格鍵與泰語元音沒有任何問題。它會刪除所有字符,包括泰語輔音和元音。

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。我真的不知道如何解決這個問題。請指教。

回答

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

相關問題