2012-11-08 41 views
3

我的應用不能正確顯示重音字符。去年我看了一下,但沒能找到解決方案。我一直在挖掘,但仍然沒有找到解決方案。我提供了一個屏幕快照,顯示當用戶在單擊按鈕時加載到WebView的EditText字段中輸入「London Ch â teau」時會發生什麼情況。重音字符「â」不會按預期翻譯。它翻譯爲「à ç」,「London Chà ç teau」。希望得到一個解決方案,提前致謝。在webview中顯示重音字符

我提供代碼來演示:

public class MainActivity extends Activity { 

private WebView webView; 
private EditText edittext_1; 
private Button buttonUpdate; 

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

    webView = (WebView) findViewById(R.id.webview_summary); 
    webView.getSettings().setJavaScriptEnabled(true); 

    edittext_1 = (EditText) findViewById(R.id.editText_1); 
    buttonUpdate = (Button) findViewById(R.id.button_update); 

    buttonUpdate.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      String s = edittext_1.getText().toString(); 
      webView.loadData(s, "text/html", "utf-8"); 
     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 
} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<WebView 
    android:id="@+id/webview_summary" 
    android:layout_width="fill_parent" 
    android:layout_height="0px" 
    android:layout_weight=".85" /> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="0px" 
    android:layout_weight=".15" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <EditText 
      android:id="@+id/editText_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_marginLeft="25dp" 
      android:layout_marginRight="25dp" 
      android:hint="text" 
      android:minWidth="100dp" 
      android:text="" /> 

     <Button 
      android:id="@+id/button_update" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@id/editText_1" 
      android:padding="10dp" /> 
    </RelativeLayout> 
</LinearLayout> 

</LinearLayout> 

回答

0

嘗試從Html.fromHtml

String s = Html.fromHtml(edittext_1.getText().toString()).toString(); 
+1

感謝取回,但沒有解決它 - 同樣的結果。 – user1808920

11

得到譯文]只要我貼我發現了一個建議,工作。我改變了以下行從:

webView.loadData(s, "text/html", "utf-8"); 

到:

webView.loadDataWithBaseURL(null, s, "text/html", "utf-8", null); 
+0

這工作完美,謝謝! – Kon

+0

完美地工作。應該將其標記爲正確的答案。 –