在android中,当我单击相应的按钮时,是否可以向前或向后移动?

在android中,当我单击相应的按钮时,是否可以向前或向后移动?,android,Android,我对安卓ImageView有疑问。我正在制作一个示例应用程序,用于在我的可绘制文件夹中显示图像。首先,我在布局中使用了Galleryview和Imageview来显示图像,当我在Galleryview中单击图像时,相应的图像将显示在Imageview中。现在我想这样实施, 也就是说,我在我的res/drawable文件夹中设置了10幅图像,这些图像将显示在相应的按钮点击之间,如果我点击Front按钮,ImageView中的图像将向前显示,否则我单击后退按钮,ImageView中的图像会向后移动。

我对安卓
ImageView
有疑问。我正在制作一个示例应用程序,用于在我的可绘制文件夹中显示图像。首先,我在布局中使用了
Galleryview
Imageview
来显示图像,当我在
Galleryview
中单击图像时,相应的图像将显示在
Imageview
中。现在我想这样实施,

也就是说,我在我的
res/drawable
文件夹中设置了10幅图像,这些图像将显示在相应的按钮点击之间,如果我点击Front按钮,
ImageView
中的图像将向前显示,否则我单击后退按钮,
ImageView
中的图像会向后移动。
是否可以这样做

我使用了以下代码,但图像不会显示

main.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" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="333dp"
        android:src="@drawable/ic_launcher" />

    <Gallery
        android:id="@+id/galleryView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/backBTN"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="B" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="imagePosition"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/frontBTN"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/backBTN"
            android:layout_alignBottom="@+id/backBTN"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="F" />
    </RelativeLayout>

</LinearLayout>

我的Java代码MainActivity.java是

package com.test.button.wallpaper;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    Button front, back;
    ImageView mImage;
    Gallery mGallery;
    int image_postion;
    private int[] gal = { R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
            R.drawable.img_5 };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        front = (Button) findViewById(R.id.frontBTN);
        back = (Button) findViewById(R.id.backBTN);
        mImage = (ImageView) findViewById(R.id.imageView);
        mGallery = (Gallery) findViewById(R.id.galleryView);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setVisibility(View.GONE);
        mGallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int loc,
                    long arg3) {
                // TODO Auto-generated method stub
                image_postion = loc;
                System.out.println("Inside the Gallery");

            }
        });

        back.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                int position = mGallery.getSelectedItemPosition();
                if (position < mGallery.getCount() - 1)
                    mGallery.setSelection(position + 1);

            }
        });

        front.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                int position = mGallery.getSelectedItemPosition();
                if (position >= 1)
                    mGallery.setSelection(position - 1);

            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        private Context ctx;
        int imageBackground;

        public ImageAdapter(Context c) {
            ctx = c;
            // TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
            // imageBackground = ta.getResourceId(
            // R.styleable.Gallery1_android_galleryItemBackground, 1);
            // ta.recycle();
        }

        @Override
        public int getCount() {

            return gal.length;
        }

        @Override
        public Object getItem(int arg0) {

            return arg0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            ImageView iv = new ImageView(ctx);
            iv.setImageResource(gal[arg0]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
            iv.setBackgroundResource(imageBackground);
            return iv;
        }

    }
}
package com.test.button.wallpaper;
导入android.app.Activity;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.BaseAdapter;
导入android.widget.Button;
导入android.widget.Gallery;
导入android.widget.ImageView;
公共类MainActivity扩展了活动{
/**在首次创建活动时调用*/
按钮前,后;
图像视图模拟图像;
马加里画廊;
int图像位置;
private int[]gal={R.drawable.img_2,R.drawable.img_3,R.drawable.img_4,
R.drawable.img_5};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
front=(按钮)findViewById(R.id.frontBTN);
后退=(按钮)findViewById(R.id.backBTN);
mImage=(ImageView)findViewById(R.id.ImageView);
mGallery=(画廊)findViewById(R.id.galleryView);
mGallery.setAdapter(新的ImageAdapter(this));
mGallery.setVisibility(View.GONE);
mGallery.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(适配器视图arg0,视图arg1,内部位置,
长arg3){
//TODO自动生成的方法存根
图像位置=loc;
System.out.println(“画廊内部”);
}
});
back.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
int position=mGallery.getSelectedItemPosition();
if(位置=1)
选举管理委员会(职位-1);
}
});
}
公共类ImageAdapter扩展了BaseAdapter{
私有上下文ctx;
int图像背景;
公共图像适配器(上下文c){
ctx=c;
//TypedArray ta=获得的StyledAttributes(R.styleable.Gallery1);
//imageBackground=ta.getResourceId(
//R.styleable.Gallery1_android_galleryItemBackground,1);
//ta.recycle();
}
@凌驾
public int getCount(){
返回gal.length;
}
@凌驾
公共对象getItem(int arg0){
返回arg0;
}
@凌驾
公共长getItemId(int arg0){
返回arg0;
}
@凌驾
公共视图getView(int arg0、视图arg1、视图组arg2){
ImageView iv=新的ImageView(ctx);
iv.setImageResource(gal[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.设置布局参数(新画廊布局参数(150120));
iv.挫折背景资源(图像背景);
回报四;
}
}
}

是的,只需通过将选择设置为位置即可

btnNext.setOnClickListener(new View.OnClickListener()
{
     public void onClick(View view)
     {
          int position=gallery.getSelectedIndex();
          if(position<gallery.getCount()-1)
            gallery.setSelection(position+1);
     }
});


btnPrevious.setOnClickListener(new View.OnClickListener()
{
     public void onClick(View view)
     {
          int position=gallery.getSelectedIndex();
          if(position>=1)
              gallery.setSelection(position-1);
     }
});
btnNext.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图)
{
int position=gallery.getSelectedIndex();
如果(位置=1)
旁听席选举(职位-1);
}
});

是的,只需通过将选择设置为位置即可

btnNext.setOnClickListener(new View.OnClickListener()
{
     public void onClick(View view)
     {
          int position=gallery.getSelectedIndex();
          if(position<gallery.getCount()-1)
            gallery.setSelection(position+1);
     }
});


btnPrevious.setOnClickListener(new View.OnClickListener()
{
     public void onClick(View view)
     {
          int position=gallery.getSelectedIndex();
          if(position>=1)
              gallery.setSelection(position-1);
     }
});
btnNext.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图)
{
int position=gallery.getSelectedIndex();
如果(位置=1)
旁听席选举(职位-1);
}
});

我也做同样的事情,它在我的应用程序中完美地工作

这个xml

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:ns="http://schemas.android.com/apk/res/com.colors.abc"   
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" >         
   <RelativeLayout
       android:id="@+id/item"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" >        
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"             
        android:background="@drawable/image" />
     <Button
         android:id="@+id/back"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"        
         android:text="small" />
    <Button
        android:id="@+id/forword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"         
        android:text="foeword" />
   </RelativeLayout>
 </RelativeLayout>

和爪哇

          setimage(present);        
        back.setEnabled(false);     
        back.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {               

            if (past==1) {
                back.setEnabled(false);
            }else {
                back.setEnabled(true);
            }
            if(past<=10) forword.setEnabled(true);

            setimage(past);                     

           }                
          });   

                forword.setOnClickListener(new OnClickListener() {              
            public void onClick(View v) {                   

                if(future==10){                 
                    forword.setEnabled(false);
                }else {
                    forword.setEnabled(true);
                }
                if(future>=2) {
                    back.setEnabled(true);
                }                   
                setimage(future);                    
            }               
        });     
setimage(存在);
back.setEnabled(false);
back.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
如果(过去==1){
back.setEnabled(false);
}否则{
back.setEnabled(true);
}
如果(过去=2){
back.setEnabled(true);
}                   
setimage(未来);
}               
});     

我也做同样的事情,它在我的应用程序中完美地工作

这个xml

<?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:ns="http://schemas.android.com/apk/res/com.colors.abc"   
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" >         
   <RelativeLayout
       android:id="@+id/item"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" >        
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"             
        android:background="@drawable/image" />
     <Button
         android:id="@+id/back"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"        
         android:text="small" />
    <Button
        android:id="@+id/forword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"         
        android:text="foeword" />
   </RelativeLayout>
 </RelativeLayout>

和爪哇

          setimage(present);        
        back.setEnabled(false);     
        back.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {               

            if (past==1) {
                back.setEnabled(false);
            }else {
                back.setEnabled(true);
            }
            if(past<=10) forword.setEnabled(true);

            setimage(past);                     

           }                
          });   

                forword.setOnClickListener(new OnClickListener() {              
            public void onClick(View v) {                   

                if(future==10){                 
                    forword.setEnabled(false);
                }else {
                    forword.setEnabled(true);
                }
                if(future>=2) {
                    back.setEnabled(true);
                }                   
                setimage(future);                    
            }               
        });     
setimage(存在);
back.setEnabled(false);
back.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
如果(过去==1){