0

有人可以帮我编写代码吗?我有包含多个图像的图像滑块,如果我选择任何特定图像并单击按钮,则所选图像应在另一个页面/活动上打开。我尝试了很多,但无法在另一个页面/活动上显示选定的图像。所以,请帮助我。提前致谢。

**Main.java**   

包 com.example.imagesliderdemo;

import android.annotation.SuppressLint;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

@SuppressWarnings("deprecation")
public class Main extends Activity {

    private Gallery topsgallery;
    private ImageView imgView;
    int GalItemBg;

    private Integer[] Imgid = {
            R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5 };



     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        imgView = (ImageView)findViewById(R.id.ImageViewTops);  
        imgView.setImageResource(Imgid[0]);

        Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
        btnNextScreen.setOnClickListener(new View.OnClickListener() {


            private int position;


            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Intent nextScreen = new Intent(Main.this, OpenImage.class);
                Intent nextScreen=new Intent(getApplicationContext(),OpenImage.class);                       
                                 nextScreen.putExtra("image",R.drawable.ic_launcher);
          startActivity(nextScreen);
            }

        });


        topsgallery = (Gallery) findViewById(R.id.gallery1);
        topsgallery.setAdapter(new AddImgAdp (this));

        topsgallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {


            imgView = (ImageView)findViewById(R.id.ImageViewTops);

            }

        }); 

     }  

   public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
            GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            typArray.recycle();
        }


        public int getCount() {
            return Imgid.length;
        }

        public Object getItem(int position) {
                return null;
        }

        public long getItemId(int position) {
                return 0;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(cont);

            imageView.setImageResource(Imgid[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imageView.setBackgroundResource(GalItemBg);      



            return imageView;             


          }
   }
}

**Other activity file OpenImage.java**
package com.example.imagesliderdemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class OpenImage extends Activity
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        ImageView image = (ImageView) findViewById(R.id.imageview);
        int image_link = getIntent().getIntExtra("image_url", R.drawable.ic_launcher);
        image.setImageResource(image_link);

        Button btnClose = (Button) findViewById(R.id.btnClose);
         btnClose.setOnClickListener(new View.OnClickListener() {

                public void onClick(View arg0) {
                    //Closing SecondScreen Activity
                    finish();
                }
            });

    }
}


**activity_main.xml**


<?xml version="1.0" encoding="utf-8"?>

<ScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:overScrollMode="always" 
        android:isScrollContainer="true" 
        android:scrollbarAlwaysDrawVerticalTrack="true" 
        android:scrollbarStyle="outsideInset" 
        android:scrollbars="vertical">

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="vertical">

<LinearLayout 
    android:id="@+id/LinearLayoutTops"
    android:layout_width="wrap_content" 
    android:layout_height="100dp"
    android:orientation="vertical">
    <TextView
        android:gravity="center"
        android:text="@string/tops"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <LinearLayout 
        android:id="@+id/LinearLayoutTopsGallery"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:orientation="horizontal">

    <Gallery 
        android:id="@+id/gallery1" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:spacing="10dp" />

    <ImageView 
        android:id="@+id/ImageViewTops"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:contentDescription="@string/imageslide"/>


    </LinearLayout>
</LinearLayout>



    <Button android:id="@+id/btnNextScreen"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Send to Next Screen"
            android:layout_marginTop="15dip"/>



</LinearLayout>    
</ScrollView>



**Another Activity screen2.xml**
<?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" >

    <Button
        android:id="@+id/btnClose"
        android:layout_width="151dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:text="Close" />

    <ImageView 
        android:id="@+id/imageview"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />


</LinearLayout>
4

4 回答 4

0

你可以使用片段, http://developer.android.com/guide/components/fragments.html 这个页面有一个例子,也许可以帮助你。

于 2013-11-07T09:14:22.970 回答
0

创建一个全局变量以获取 Slider 的位置(假设该变量为 pos)。现在在您的滑块适配器代码的 getView 中,如下所示

public View getView(final int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(cont);

            pos=position;

            imageView.setImageResource(Imgid[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imageView.setBackgroundResource(GalItemBg);      



            return imageView;



          }

现在在按钮点击代码是这样的:

btnNextScreen.setOnClickListener(new View.OnClickListener() {


            private int position;


            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Intent nextScreen = new Intent(Main.this, OpenImage.class);
                Intent nextScreen=new Intent(getApplicationContext(),OpenImage.class);

                //Sending data to another Activity
                 nextScreen.putExtra("id",Imgid[pos]);
                Log.e("n", Imgid[pos]);
                startActivity(nextScreen);
            }

        });
于 2013-11-07T09:40:20.243 回答
0

只需在单击侦听器上传递图库中选定的图像 id 值即可。

    topsgallery.setOnItemClickListener(new OnItemClickListener()
   {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
    {
        // TODO Auto-generated method stub
     Intent fullImage = new Intent(YourActivity.this,FullImageView.class)
     fullImage.putExtra("image_url",R.drawable.image);
     startActivity(fullImage);

    }
});

考虑以下步骤:

#1 创建一个新的活动,比如 ex :FullImageView使用以下视图并在清单中声明。

<?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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android" />
 </ImageView>
</LinearLayout>

3:在FullImageView活动的onCreate中接收intent

  ImageView image = (ImageView) findViewById(R.id.image);
  int image_link = getIntent().getIntExtra("image_url", R.drawable.default);

4:将 URL 设置为您的 ImageView。

imageView.setImageResource(image_link);

如果您遇到任何与此相关的问题,请随意。

于 2013-11-07T09:36:49.057 回答
0

您可以将意图放入适配器中......然后执行以下操作(请注意,您需要将上下文传递给适配器):

public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(cont);

        imageView.setImageResource(Imgid[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(80, 70));
        imageView.setBackgroundResource(GalItemBg);

        imageView.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
               Intent nextScreen=new Intent(context,OpenImage.class);                       
            nextScreen.putExtra("image",ImgId[pos]);
      startActivity(nextScreen);
        }
    });


        return imageView;             


      }
于 2013-11-07T13:38:33.703 回答