我的应用程序无法正确显示重音字符。我去年对此进行了调查,但未能找到解决方案。我一直在四处寻找,但仍未找到解决方案。我提供了一个屏幕截图,显示当用户在 EditText 字段中输入“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>
最佳答案
我一发帖就发现了一个可行的建议。我更改了以下行:
webView.loadData(s, "text/html", "utf-8");
到:
webView.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);
关于android - 在 webview 中显示重音字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13287985/