我有一个包含 3 个图像/图标的 GridView 布局。我想要做的是,每当单击一个项目时,都应该显示一个网站页面(Webview)。这是我所做的:
- 在 main.xml 文件中定义了 GridView 和以 LinearLayout 作为根元素的 WebView 布局。
- 在Androidmanifest.xml文件中赋予了上网权限
- 为gridview编写代码
- 为 Webview 编写代码。
- 在 GridView 中,在 setOnItemClickListener 下,使用 loadUrl() 给出要显示的网页的 url
所以问题是:“如何在单击图标/图像时显示网页?” 我可以在模拟器上看到 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 代码:
<?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 代码
包在.niteesh.MultipleActivity;
导入android.app.Activity;导入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 但没有显示网页。我尝试了各种周转,但找不到解决方案/