2013-11-20 139 views
0

我有一個瘋狂的問題。在我XML,我有2個ButtonsAndroid按鈕點擊顯示webView

每個Button的Facebook和Twiiter

,並在相同的佈局我有一個WebView

當我點擊Facebook的Button,它會打開Facebook在WebView 和同去對於推特

這是我的佈局代碼:

頁面。 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

所以最新你的問題?你想要達到什麼? –

+0

正如你可以看到,在我的Xml文件2 Facebook和Twitter和WebView的按鈕確定,現在我想當我點擊任何人按鈕顯示Facebook或twiiter網站在WebView在相同的Xml文件 – user2983357

回答

0

進行以下更改:

private WebView webView; 
webView = (WebView) findViewById(R.id.webView1); 
webView.getSettings().setJavaScriptEnabled(true); 

ON按鈕點擊:

webView.loadUrl("http://www.facebook.com"); 

我希望它可以工作。

+0

它不能正常工作 – user2983357

+0

你是否需要權限<使用權限android:name =「android.permission.INTERNET」/> – bGorle

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); 
    } 
} 
+0

不工作sir – user2983357

+0

你是否需要權限 <使用權限android:name =「android.permission.INTERNET 「/> –

+0

完成了花花公子................... –