我有一个包含3个图像/图标的GridView布局。我想要做的是,无论何时点击一个项目,都应该显示一个网站页面(Webview)。 这是我做的:
所以问题是:“当点击图标/图像时,如何显示网页?” 我能够在模拟器上看到UI。但点击图标没有任何回应。我想在onClickItem()事件上做错了。
感谢您的时间。 我写了以下代码:
package prototype.wenview.gridview;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity
{
// Images to display-----------------------//
Integer[] imageIds = { R.drawable.market, R.drawable.news, R.drawable.weather };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //calls the method from super class Activity
setContentView(R.layout.main); //sets the UI
GridView gridView = (GridView) findViewById(R.id.gridview);// Bring gridView into context from main.xml
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
WebView myWebView = (WebView)findViewById(R.id.webview);
myWebView.loadUrl("http://translate.google.com/translate?sl=auto&tl=hi&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fwww.kvk.pravara.com%2Fweather.html");
}
}
); //setOnItemClickListener declaration ends here.
} //onCreate() ends here.
public class ImageAdapter extends BaseAdapter
{
private Context context; // Context class object - context
public ImageAdapter(Context c)
{
context = c;
}
//return number of images
public int getCount()
{
return imageIds.length;
}
public Object getItem(int position) // return type of getItem method is an Object
{
return position;
}
public long getItemId(int position)
{
return position;
}
//returns an ImageView view--------------
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if(convertView == null)
{
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIds[position]);
return imageView;
}
}
}
XML CODE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
/ * Activity2.java的XML代码。此页面旨在在单击网格视图中的图标后显示网页。 * /
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
//活动2的Java代码
package in.niteesh.MultipleActivities;
import android.app.Activity; import android.os.Bundle;
public class Activity2 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //calls the method from super class Activity
setContentView(R.layout.activity2); //sets the UI
}
}
// AndroidManifest.xml文件,其中我提到了Activity2的存在
<activity android:name=".Activity2" android:label="Activity 2">
<intent-filter>
<action android:name="net.learn2develop.ACTIVITY2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
//在onItemClick下的MainActivity类中,我为第二个Activity中显示的网页编写了以下代码
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String url = "http://google.com/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(new Intent("net.learn2develop.ACTIVITY2"));//reference to start the activity
}
/ 现在问题是 - 点击图标后,它会切换到Activity2,但不会显示任何网页。我尝试了各种转变但无法找到解决方案 /
答案 0 :(得分:1)
在xml代码中,webview和网格视图的高度为填充父级。 你必须能够加载页面,但gridview是fillparent它占据整个屏幕。
让它包装内容。
而且我认为如果满足您的要求,最好在下一个活动中加载webview。
答案 1 :(得分:1)
Agarwal是对的 - 考虑修改您的布局,或者在单独的活动中打开网页。
以下是从网址打开网页的代码:
String url = "http://almondmendoza.com/android-applications/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
编辑:
从新活动中打开链接:
String url = "http://almondmendoza.com/android-applications/";
Intent i = new Intent(this, Activity2.class);
i.putExtra("key_url", url);
startActivity(i);
this
中的 new Intent(this, Activity2.class);
是您活动的背景。如果此代码位于内部类中,请使用MyActivity1.this
for Activity2,获取Intent从之前的startActivity
代码中抛出的url,
String url = getIntent().getStringExtra("key_url");
然后你可以用网址做任何你想做的事情,比如
webView.loadUrl(url);
onCreate
答案 2 :(得分:1)
我有一个解决方案来打开您活动中的网页:
首先,我改变了你布局的一些属性:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
否则WebView将不可见。
您还必须添加一个类来控制操作系统:
private class CustomWebViewClient extends WebViewClient {
@Override
/**
* Take control over the loaded url so the url is open in the current Webview and not by the OS browser
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
你的听众应该是这样的:
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
WebView myWebView = (WebView)findViewById(R.id.webview);
CustomWebViewClient myCustomWebViewClient = new CustomWebViewClient();
myWebView.setWebViewClient(myCustomWebViewClient);
myWebView.loadUrl("http://www.google.com");
}
希望这是你正在寻找的东西。
编辑:另一个解决方案,在第二个活动(应用程序)中打开webview:
我刚刚给你的听众做了一些改变:
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String url;
//The url changes accordingly to the button clicked
switch (position) {
case 0: url = "http://stackoverflow.com";
break;
case 1: url = "http://yahoo.fr";
break;
case 2: url = "http://bing.com";
break;
default: url = "http://google.com";
break;
}
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
//used to pass the url to the next activity
intent.putExtra("url", url);
startActivity(intent);//reference to start the activity
}
我的第二个活动:
public class SecondActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); //calls the method from super class Activity
setContentView(R.layout.second); //sets the UI
//the webview inside the second activity xml
WebView myWebView = (WebView)findViewById(R.id.webview);
CustomWebViewClient myCustomWebViewClient = new CustomWebViewClient();
myWebView.setWebViewClient(myCustomWebViewClient);
//get the url from the previous activity
String url = getIntent().getStringExtra("url");
myWebView.loadUrl(url);
}
private class CustomWebViewClient extends WebViewClient {
@Override
/**
* Take control over the loaded url so the url is open in the current Webview and not by the OS browser
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
}
我没有触摸xml文件。
至于第一个解决方案,我们需要覆盖ordre中的shouldOverrideUrlLoading以在右视图中加载url。