English 中文(简体)
将网格布局图像转换为按钮, 以打开每个 [重复] 的超速活动
原标题:Making grid layout images into buttons to open a seprate activity each [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Create a clickable image in a GridView in Android

我设置了一个网格布局,但想让图像打开独立的活动,因为我的代码看起来是这样的:

package android.grid.layout;

import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter { private Context mContext;

// Keep all Images in array
public Integer[] mThumbIds = {
        R.drawable.group_off, R.drawable.indoor_off, R.drawable.outdoors_off,
        R.drawable.attractions_off, R.drawable.playcentre_off, R.drawable.animals_nature_off,
        R.drawable.entertainment_off, R.drawable.arts_off, R.drawable.educational_off, 
        R.drawable.museum_off, R.drawable.historical_off, R.drawable.exercise_off,
        R.drawable.swimming_off, R.drawable.restaurant_off, R.drawable.partyhat_off
时 时;

// Constructor
public ImageAdapter(Context c){
    mContext = c;
时 时

@Override
public int getCount() {
    return mThumbIds.length;
时 时

@Override
public Object getItem(int position) {
    return mThumbIds[position];
时 时

@Override
public long getItemId(int position) {
    return 0;
时 时

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
    return imageView;
时 时

时 时

和阅读的类 它看起来像这个

package android.grid.layout;

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView;

家庭活动扩展 活动 {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

时 时   

时 时

我该如何让每个点击都能够打开新的独立活动,

问题回答

在Adpater S GetView 内部添加点击听众

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
   imageView.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View view) {
   Intent i=new Intent(context,activity.class);
     startActivity(i);
  }

});

    return imageView;
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签