打开网页视图
问题描述:
我有一个图片按钮,点击后会打开一个新的网页浏览视图.Web视图将显示一些自定义HTML。当我点击按钮时,Web视图不显示任何内容。打开网页视图
这是我的按钮查看代码: -
public void GoToContact(View view)
{
WebView webView;
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.myWebView);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<html><body><h1>Hello, WebView</h1></body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
}
THIS IS MY WEB页面的版面: -
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/GoToHome"
android:onClick="GoToTC"
android:background="@drawable/test"
android:layout_width="360dp"
android:layout_height="60dp"
android:contentDescription="@string/header"/>
<WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="400dp" />
</LinearLayout>
答
首先声明的WebView为您的活动类成员:
WebView webView;
public void GoToContact(View view)
{
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.myWebView);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<html><body><h1>Hello, WebView</h1></body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
}
然后再试一次... btw请向我们展示您的layout.xml和cod整个活动课的内容。
答
试试这个
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
});
}
}
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
你可以发布你的布局呢? – Gyonder 2013-03-21 10:26:39