【问题标题】:image switching on button click图像切换按钮点击
【发布时间】:2011-09-18 15:35:00
【问题描述】:

我尝试在按钮单击时切换图像..但我失败了并得到了错误 这是我的代码....请有人帮助我! 如果需要,请添加 XML 代码,也请发布相同的代码

package com.conn;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher.ViewFactory;

public class image_slider extends Activity
{

        Integer[] imageIDs = { R.drawable.haha,  R.drawable.dte,R.drawable.new_login };
        private ImageSwitcher imageSwitcher;
        private Button nextButton;
        private Button previousButton; 

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.imgslide);
            imageSwitcher.setImageResource(imageIDs[0]);
            nextButton = (Button) findViewById(R.id.next);
            nextButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    final Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);       
                    final Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);     
                    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);      
                    imageSwitcher.setFactory(this);   
                    imageSwitcher.setInAnimation(in);      
                    imageSwitcher.setOutAnimation(out);             
                    imageSwitcher.setImageResource(imageIDs[1]);
//                  imageSwitcher.setImageResource(imageIDs[1]);

                }

            }); 

            previousButton = (Button) findViewById(R.id.previous);
            previousButton.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    final Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_left);
                    final Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_right);
                    imageSwitcher.setFactory(this);
                    imageSwitcher.setInAnimation(in);
                    imageSwitcher.setOutAnimation(out);
                    imageSwitcher.setImageResource(imageIDs[0]);

                }

            });
        }
public View makeView()
    {
        ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(0xFF000000);
                imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return imageView;
    }       

}

【问题讨论】:

  • 你遇到了什么错误?
  • loadAnimation of previous button 我得到了 AnimationUtils 类型中的方法 loadAnimation(Context, int) 不适用于参数 (new View.OnClickListener(){}, int)
  • 同样在“slide_out_left”和“slide_in_right”红色下划线表示“slide_in_right/slide_out_left 无法解析或不是字段”
  • 再次感谢你...关于 setFactory !!!!!!!!!!!1

标签: android image switching buttonclick


【解决方案1】:

您的问题是,当您在 OnClickListener 中引用 this 时,您指的是该特定侦听器,而不是您当前的活动。你应该把它改成

 final Animation out= AnimationUtils.loadAnimation(image_slider.this, android.R.anim.slide_out_right); 
 final Animation in= AnimationUtils.loadAnimation(image_slider.this, android.R.anim.slide_in_left);
 ...
 imageSwitcher.setFactory(image_slider.this);

并且使用 android.R.anim.slide_in_leftandroid.R.anim.slide_out_right 而不是 slide_out_left 和 "slide_in_right",因为它们不存在。

顺便说一句,Java 的好习惯是以大写字母开头的类名,例如ImageSlider

【讨论】:

  • 06-20 19:03:41.111: WARN/ActivityManager(58): HistoryRecord{45148388 com.conn/.image_slider} 的活动销毁超时现在我在运行时遇到此错误...呵呵...请帮助..
  • 检查this post 以获得解决方案,或为其创建一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
相关资源
最近更新 更多