我有一个疯狂的问题。在我的XML
中,我有2 Buttons
Facebook和Twiiter的每个Button
并且在相同的布局中我有一个WebView
当我点击Facebook Button
时,它会在WebView
中打开Facebook
同样适用于Twitter
这是我的布局代码:
pages.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background2" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="@string/page"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#3600ff" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/buttonFace" />
<Button
android:id="@+id/buttonFace"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonTwitter"
android:layout_alignBottom="@+id/buttonTwitter"
android:layout_alignParentRight="true"
android:text="@string/facebook" />
<Button
android:id="@+id/buttonTwitter"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="@string/twitter" />
</RelativeLayout>
答案 0 :(得分:0)
做出这些改变:
private WebView webView;
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
ON按钮单击:
webView.loadUrl("http://www.facebook.com");
我希望它可行。
答案 1 :(得分:0)
这个xml代码
调用此意图
Intent faq = new Intent(this,WebViewActivity.class);
faq.putExtra("url", "Your URL");
faq.putExtra("header_text", "Heading");
startActivity(faq);
<include
android:id="@+id/incld"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header" />
<WebView
android:id="@+id/webview01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</WebView>
这是java代码
public class WebViewActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
private TextView header_maintext;
private TextView headeroptiontext;
private RelativeLayout back;
/** Called when the activity is first created. */
@SuppressLint("SetJavaScriptEnabled") @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview_layout);
url_string = getIntent().getStringExtra("url");
header_maintext_string = getIntent().getStringExtra("header_text");
webview = (WebView)findViewById(R.id.webview01);
header_maintext= (TextView)findViewById(R.id.header_maintext);
header_maintext.setText(header_maintext_string);
headeroptiontext = (TextView)findViewById(R.id.headeroptiontext);
headeroptiontext.setVisibility(View.GONE);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
back = (RelativeLayout) findViewById(R.id.back_layout);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(webview.canGoBack() == true)
{
webview.goBack();
}
else
{
finish();
}
}
});
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(WebViewActivity.this, "tittle", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(WebViewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl(url_string);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}