2014-09-13 87 views
1

嗨,我是android新手。Android主要活动有2个按钮在webview中打开第二个活动

我的主要活动有2个按钮。如果用户点击按钮1,它将在webview中打开谷歌网站,按钮2在webview中打开雅虎网站。我不想在任何其他浏览器中打开。

这是我的代码;但它不工作,任何人都可以告诉我,我在这里做错了什么。

package com.jo.mavselect; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.content.Intent; 
import android.view.View; 
import android.widget.Button; 

public class SelectRm extends Activity { 
    public String message; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_select_rm); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.select_rm, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


    /** Called when the user clicks the Send button */ 
    public void sendMessage(View v) { 


       if(v.getId()==R.id.B9) 
       { 
        message ="www.google.com.au"; 
       } 
      else 
       if(v.getId()==R.id.B10) 
       { 
        message ="www.google.com.au"; 
       } 

     Intent intent = new Intent(this, MavisActivity.class); 
     intent.putExtra("msg",message); 
     startActivity(intent); 

    } 



} 

** ** MavisActivity

package com.jo.mavselect; 


import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.content.Intent; 



public class MavisActivity extends Activity { 


    private WebView mWebView; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     mWebView = new WebView(this); 
       setContentView(mWebView); 
     mWebView =(WebView) findViewById(R.id.Webview); 


     mWebView.setWebViewClient(new WebViewClient()); 

     // web page to fit to the screen 
     mWebView.getSettings().setLoadWithOverviewMode(true); 
     mWebView.getSettings().setUseWideViewPort(true); 

     // Not to cache 
     mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
     mWebView.getSettings().setAppCacheEnabled(false); 

     // Enable Java script 
     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 

     // No to cache 
     mWebView.clearCache(true); 
     //Clears any saved data for web forms. 
     mWebView.clearFormData(); 
     //Tells this WebView to clear its internal back/forward list. 
     mWebView.clearHistory(); 

     Intent intent = getIntent(); 
     String ur = intent.getExtras().getString("msg"); 

     mWebView.loadUrl(ur); 


     final Activity activity = this; 
     mWebView.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       mWebView.loadUrl("file:///android_asset/error.html"); 
      } 
     }); 


    } 


} 

清单文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.jo.mavselect" > 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 


      android:name="com.jo.mavselect.MavisActivity" 
      android:label="MavisActivity" 
      android:parentActivityName="com.jo.mavselect.SelectRm" > 
      // android:label="@string/app_name" > 

      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.jo.mavselect.SelectRm" /> 





      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

    <uses-permission android:name="android.permission.INTERNET" /> 

</manifest> 

活动xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".SelectRm/"> 



    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Goole" 
     android:id="@+id/B9" 
     android:layout_marginTop="60dp" 
     android:layout_alignParentTop="true" 
     android:onClick="sendMessage" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="yahoo" 
     android:id="@+id/B10" 
     android:layout_toEndOf="@+id/B9" 
     android:layout_marginLeft="84dp" 
     android:layout_alignTop="@+id/B9" 
     android:layout_toRightOf="@+id/B9" 
     android:onClick="sendMessage" /> 

     <WebView 
     android:id="@+id/Webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 



</RelativeLayout> 
+1

你不需要这个'mWebView =(的WebView)findViewById(R.id.Webview);' – Raghunandan 2014-09-13 14:32:57

回答

3

你完全错误的方式。

  1. 您应该尝试修复您的布局。删除此行代码

    mWebView = new WebView(this); 
    setContentView(mWebView);
    并替换为
     setContentView(R.layout.activity_xml);
    在xml文件中,在您的web视图元素中添加此行代码。
    android:layout_below="@id/B9"

  2. 您必须为这些按钮添加onClickListener方法。在这个监听器中设置用web视图打开的页面。在活动类 添加方法:

    public void sendMessage(View v) 
    { 
        if(v.getId() == R.id.B9){ 
         mWebView.loadUrl(" http://www.google.com "); 
        } 
        if(v.getId() == R.id.B10) { 
         mWebView.loadUrl(" http://www.yahoo.com "); 
        } 
    }

+0

谢谢。是的,我真的搞砸了布局文件。它正在工作。再一次感谢你 – Jro 2014-09-14 14:03:31

相关问题