webView ไม่ขึ้นภาษาไทย Home Forums General Android Application Development webView ไม่ขึ้นภาษาไทย Khontai / Reader Posted on April 29, 2012 at 5:24 pm Share Tweet Line ลองทำโปรแกรมโดยโหลด webView จากคำสั่งString customHTML = “สวัสดี, WebView”; webView.loadData(customHTML, “text/html”, “UTF-8”);แต่พอรันแล้ว ภาษาไทยอ่านไม่ออกไม่ทราบว่าจะแก้ไขได้อย่างไรครับขอบคุณครับ 7 Replies sopimp / Reader Posted on April 30, 2012 at 4:54 pmถ้า hard code จะไม่ได้ครับ ภาษาไทยควรใส่ไว้ใน String.xml แล้ว get resource ออกมา ถึงจะเป็นภาษาไทย Khontai / Reader Posted on May 2, 2012 at 3:00 amขอบคุณสำหรับคำแนะนำครับแต่ลองดูแล้ว ก็ยังไม่ได้ครับพอรันแล้ว ภาษาไทยก็ยังอ่านไม่ออกเหมือนเดิมครับ sopimp / Reader Posted on May 2, 2012 at 8:04 pmtest ด้วยอะไรครับ Khontai / Reader Posted on May 3, 2012 at 4:18 amtest ทั้งใน Emulator และในเครื่องจริงเลยครับ Khontai / Reader Posted on May 3, 2012 at 4:52 ampackage test.android.custwebview;import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.webkit.WebView; import android.widget.Button; import android.widget.FrameLayout;public class CustomWebViewActivity extends Activity { /** Called when the activity is first created. */ private Button btnWebview; private WebView myWebView; private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnWebview = (Button) findViewById(R.id.btnWebview); setOnEvent(); } private void setOnEvent(){ btnWebview.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub ShowWebView(); } }); } private void ShowWebView(){ this.setContentView(R.layout.webview1); this.myWebView = (WebView) this.findViewById(R.id.webView); FrameLayout mContentView = (FrameLayout) getWindow(). getDecorView().findViewById(android.R.id.content); final View zoom = this.myWebView.getZoomControls(); mContentView.addView(zoom, ZOOM_PARAMS); zoom.setVisibility(View.GONE); String cText = “สวัสดี WebView “; cText += this.getResources().getString(R.string.test_hello); //load thai text from String variable this.myWebView.loadData(cText, “text/html”, “UTF-8”); } } Khontai / Reader Posted on May 3, 2012 at 4:54 am< ?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" android:layout_width="fill_parent" android:layout_height="fill_parent" /> Khontai / Reader Posted on May 4, 2012 at 7:07 pmแก้ไขได้แล้วครับต้องไปใส่ encoding=”utf-8″ ลงไปในตัวแปรด้วย จึงจะแสดงภาษาไทยครับขอขอบคุณทุกๆ คนที่ให้ความช่วยเหลือครับ
ถ้า hard code จะไม่ได้ครับ
ภาษาไทยควรใส่ไว้ใน String.xml แล้ว get resource ออกมา
ถึงจะเป็นภาษาไทย
ขอบคุณสำหรับคำแนะนำครับ
แต่ลองดูแล้ว ก็ยังไม่ได้ครับ
พอรันแล้ว ภาษาไทยก็ยังอ่านไม่ออกเหมือนเดิมครับ
test ด้วยอะไรครับ
test ทั้งใน Emulator และในเครื่องจริงเลยครับ
package test.android.custwebview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
public class CustomWebViewActivity extends Activity {
/** Called when the activity is first created. */
private Button btnWebview;
private WebView myWebView;
private static final FrameLayout.LayoutParams ZOOM_PARAMS =
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnWebview = (Button) findViewById(R.id.btnWebview);
setOnEvent();
}
private void setOnEvent(){
btnWebview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ShowWebView();
}
});
}
private void ShowWebView(){
this.setContentView(R.layout.webview1);
this.myWebView = (WebView) this.findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.myWebView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
String cText = “สวัสดี WebView “;
cText += this.getResources().getString(R.string.test_hello); //load thai text from String variable
this.myWebView.loadData(cText, “text/html”, “UTF-8”);
}
}
< ?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" android:layout_width="fill_parent" android:layout_height="fill_parent" />
แก้ไขได้แล้วครับ
ต้องไปใส่ encoding=”utf-8″ ลงไปในตัวแปรด้วย จึงจะแสดงภาษาไทยครับ
ขอขอบคุณทุกๆ คนที่ให้ความช่วยเหลือครับ