如何用两个按钮打开webView?第一个按钮"google",第二个按钮"yahoo" Android项目


package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1=(Button) findViewById(R.id.button1);
        Intent intent1 = new Intent (MainActivity.this,web.class);
        intent1.putExtra("page1", "file:///android_asset/1.html");
        MainActivity.this.startActivity(intent1);
        Button b2=(Button) findViewById(R.id.button2);
        Intent intent2 = new Intent (MainActivity.this,web.class);
        intent2.putExtra("page2", "file:///android_asset/2.html");
        MainActivity.this.startActivity(intent2);


    }

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

下面是简化后的代码。您不需要创建任何意图。同时gettintent()只读page关键字。

Button b1=(Button) findViewById(R.id.button1);
Button b2=(Button) findViewById(R.id.button2);
Intent intent = new Intent (this,web.class);
b2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
      intent.putExtra("page", "file:///android_asset/2.html");
      startActivity(intent);
        }
    });
 b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
      intent.putExtra("page", "file:///android_asset/1.html");
      startActivity(intent);
        }
    });
在web.class

Intent intent = getIntent();
String pageToLoad = intent.getStringExtra("page");

加载到pageToLoad webview

主要布局

<LinearLayout 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:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnGoogle"
        android:text="Google"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnYahoo"
        android:text="Yahoo"/>
</LinearLayout>

Web View Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></WebView>
</LinearLayout>

主要活动
public class MainActivity extends Activity implements View.OnClickListener {
private Button google;
private Button yahoo;
static final String TAG_ID="id";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    google= (Button) findViewById(R.id.btnGoogle);
    yahoo = (Button) findViewById(R.id.btnYahoo);
    google.setOnClickListener(this);
    yahoo.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    if(v.getId()==R.id.btnGoogle)
    {
        Intent google  = new Intent(this,WebViewActivity.class);
        google.putExtra(TAG_ID,"http://www.google.com/");
        startActivity(google);
    }
    if(v.getId()==R.id.btnYahoo)
    {
        Intent yahoo  = new Intent(this,WebViewActivity.class);
        yahoo.putExtra(TAG_ID,"http://www.yahoo.com/");
        startActivity(yahoo);
    }
    }
}

Web View Activity

    public class WebViewActivity extends Activity {
    private WebView browser;
    static final String TAG_ID="id";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view_layout);
        browser = (WebView) findViewById(R.id.webview);
        browser.setWebViewClient(new MyBrowser());
        Intent intent = getIntent();
        String url=intent.getStringExtra(TAG_ID);
        if(!url.equals("")){
            open(browser,url);
        }
        else{
            Toast.makeText(this,"Error Occurred",Toast.LENGTH_SHORT).show();
            this.finish();
        }
    }
    public void open(View view,String URL){
        String url = URL;
        browser.getSettings().setLoadsImagesAutomatically(true);
        browser.getSettings().setJavaScriptEnabled(true);
        browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        browser.loadUrl(url);
    }
    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

在你的舱单

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

应用程序内部标签

<activity android:name=".WebViewActivity"></activity>

希望这将帮助您!!!!!

最新更新