import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity implements View.OnClickListener {
Button btn1, btn2, //etc..;
WebView webview_1, webview_2, //etc..;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(this);
Button btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(this);
//and so on
WebView one = (WebView) findViewById(R.id.webview1);
one.getSettings().setJavaScriptEnabled(true);
one.loadUrl("www.example.com/jpg);
WebView two = (WebView) findViewById(R.id.webview2);
two.getSettings().setJavaScriptEnabled(true);
two.loadUrl("www.example.com/jpg);
//etc
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
setContentView(R.layout.layout1);
return;
case R.id.button2:
setContentView(R.layout.layout2);
return;
//etc
}
//main_layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<LinearLayout
android:layout_width="450dip"
android:layout_height="450dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/norge"
android:orientation="vertical"
android:padding="150dp" >
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:gravity="center"
android:text="@string/overskrift_velg_sensor"
android:textSize="25dp"
tools:context=".Main" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:linksClickable="true"
android:text="@string/button2" />
</LinearLayout>
//layout1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Object"
android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/webview1"
android:layout_weight="4">
</WebView>
</LinearLayout>
//layout2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Object2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/webview2"
android:layout_weight="4">
</WebView>
</LinearLayout>
答案 0 :(得分:0)
您只能在活动的setContentView()
方法中拨打onCreate()
一次。对该方法的后续调用将不起作用。
我建议你只有一个WebView,当你点击一个按钮时,你用loadUrl()
方法改变了WebView的URL。