【问题标题】:Android Main activity with 2 button to open the 2nd activity in webview带有 2 个按钮的 Android 主要活动可在 webview 中打开第二个活动
【发布时间】:2014-11-07 13:41:10
【问题描述】:

您好,我是安卓新手。

我的主要活动有显示 2 个按钮。如果用户单击按钮 1 以在 webview 中打开 google 网站,而按钮 2 在 webview 中打开 yahoo 网站。我不想在任何其他浏览器中打开。

这是我的代码;但它不起作用,谁能告诉我我在这里做错了什么。

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>

【问题讨论】:

  • 你不需要这个mWebView =(WebView) findViewById(R.id.Webview);

标签: android android-activity webview


【解决方案1】:

你的做法完全错误。

  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("@987654321@");
        }
        if(v.getId() == R.id.B10) {
            mWebView.loadUrl("@987654322@");
        }
    }

【讨论】:

  • 谢谢。是的,我真的搞砸了布局文件。它现在正在工作。再次感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 2015-05-02
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多