我在活动WebViewActivity中有WebView,在main_activity中有一些图像按钮。 你能帮我吗?我需要点击imagebutton1,它将在WebView等google.com中打开WebViewActivity。当我点击imagebutton2时,它会打开等yahoo.com。我是编程新手,我不是英语国家(我的英语很糟糕,因为我来自捷克代表)。谢谢你的帮助!
主要活动
package com.tona.arcig;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
ImageButton imgbutton1 = (ImageButton) findViewById(R.id.imageButton1);
imgbutton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "I like trains", Toast.LENGTH_LONG).show();
}
});
}
}
WEBVIEWACTIVITY
package com.tona.arcig;
import android.app.Activity;
import android.os.Bundle;
public class WebviewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
}
}
ACTIVITY MAIN XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="@string/suplovani"
android:src="@drawable/suplovani" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="@string/rozvrh"
android:src="@drawable/rozvrh" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="@string/prihlasovani"
android:src="@drawable/prihlasovani" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="@string/email"
android:src="@drawable/email" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="@string/email"
android:src="@drawable/moodle" />
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="@string/kdm"
android:src="@drawable/jidelna" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/o_aplikaci"
android:src="@drawable/about" />
</LinearLayout>
</LinearLayout>
ACTIVITY WEBVIEW XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
清单
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
STRINGS
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="suplovani">Suplování</string>
<string name="rozvrh">Rozvrh hodin</string>
<string name="prihlasovani">Přihlašování do systému</string>
<string name="email">Email</string>
<string name="moodle">Moodle</string>
<string name="kdm">KDM</string>
<string name="o_aplikaci">O Aplikaci</string>
<string name="app_name">Arcig.CZ</string>
<string name="title_webview">Arcig.CZ</string>
</resources>
谢谢。
答案 0 :(得分:0)
这是你应该做什么的基本框架。 首先,您应该根据您点击的按钮传递一些数据(例如url string)。
接下来,您应检索数据并打开相应的网址。 以下是如何实现这一目标的。
Intent openWebViewActivity = new Intent(this, WebViewActivity.class); // this is current Activity
openWebViewActivity.putExtra("web_url", "google.com"); // openWebViewActivity.putExtra("web_url", "yahoo.com");
startActivity(openWebViewActivity);
现在将以适当的意图打开新活动。 你可以从中获得网址。
package com.tona.arcig;
import android.app.Activity;
import android.os.Bundle;
public class WebviewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String urlToLoad = getIntent().getStringExtra("web_url");
WebView webView = (WebView) findViewById(R.id.webview);
webView.loadUrl(urlToLoad);
}
}