这个也比较简单了,直接上代码。。。
我们的strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">test_webview_html</string> <string name="btn">点击执行</string> <string name="et">在此输入HTML代码</string> </resources>
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" android:selectAllOnFocus="true" android:text="@string/et"/> <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/btn"/> <WebView android:id="@+id/wv" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
我们的Activity类MainActivity类
package xiaohang.zhimeng; import org.apache.http.protocol.HTTP; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private Button btnExe; private EditText etHtml; WebView wv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnExe = (Button) findViewById(R.id.btn); etHtml = (EditText) findViewById(R.id.et); wv = (WebView) findViewById(R.id.wv); btnExe.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String htmlcode = etHtml.getText().toString(); try { wv.loadData(htmlcode, "text/html", HTTP.UTF_8); } catch (Exception e) { Toast.makeText(MainActivity.this, "错误:" + e.getMessage(), Toast.LENGTH_LONG); } } }); } }
运行效果如下
源码
1 楼
sacredlove
2011-06-08
Android技术交流超级群