单击

时间:2016-10-24 09:25:17

标签: android

我正在尝试显示一些图像以及相关的文本,但似乎无法弄清楚如何通过ImageButton onclick事件正确地循环它们。我想做的就是每次点击按钮都转到下一个图像。一旦我到达终点,就重新开始。

这是我的代码。它可能不是最优雅的,但似乎有效,除了“currentid”以某种方式改变,所以我永远不会在foreach循环中找到当前的图像。

public class Fundamentals extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fundamentals);

    //Ojbects for forward and back buttons
    ImageButton forwardButton = (ImageButton) findViewById(R.id.forward_button);

    //image object
    ImageView imageview = (ImageView) findViewById(R.id.fundamentals_image);

    //Object for picture text
    TextView textview = (TextView) findViewById(R.id.fundamentals_text);

    //Images and associated text
    Map<Integer, String> fundamentalPics = new LinkedHashMap<Integer, String>();
    fundamentalPics.put(R.drawable.fund1, "some interesting text");
    fundamentalPics.put(R.drawable.fund2, "more interesting text");
    fundamentalPics.put(R.drawable.fund3, "and more");

    //Set initial image
    Map.Entry<Integer,String> pic = (Map.Entry<Integer, String>) fundamentalPics.entrySet().iterator().next();
    imageview.setImageResource(pic.getKey());
    textview.setText(pic.getValue());

    imageview.setTag(fundamentalPics);
    }

    public void setImage(View v) {
    //Object for picture text
    TextView textview = (TextView) findViewById(R.id.fundamentals_text);

    //Object for picture image
    ImageView imageview = (ImageView) findViewById(R.id.fundamentals_image);


    //get current id
    Integer currentid = imageview.getId();

    Map<Integer, String> fundamentalPics = (LinkedHashMap<Integer, String>) imageview.getTag();

    Boolean foundImage = false;
    for (Map.Entry<Integer, String> pic : fundamentalPics.entrySet())
    {
        if (foundImage.equals(true)) {
            textview.setText(pic.getValue());
            imageview.setImageResource(pic.getKey());
            break;
        }
        //cycle until we find current image
        if (pic.getKey().equals(currentid)) {
            foundImage = true;

            if (!fundamentalPics.entrySet().iterator().hasNext()) {
                //we've reached the end.  go to beginning....
                Map.Entry<Integer,String> item = (Map.Entry<Integer, String>) fundamentalPics.entrySet().toArray()[0];
                textview.setText(item.getValue());
                imageview.setImageResource(item.getKey());
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你在比较错误:

if (pic.getKey().equals(currentid))

在地图中,您存储了drawable id,它是图像的id,因此,getKey()将返回可绘制图像的id和currentid具有imageview的id,该id在布局xml中为其定义imageview(而不是图像的可绘制ID)。虽然两者都是整数,但考虑到差异,一个来自R.drawable。*而另一个来自R.id. *,一个是每个图像的id,另一个是imageview的id。

因此,imageview是渲染图像的容器,并且您正在将此容器的id与图像ID进行比较,显然,它们不会匹配。

不幸的是,imageview没有getDrawableId()。解决方法是与每个可绘制的id进行比较:

你可以得到像

这样的抽象
Drawable myDrawable = imageview.getDrawable();

您可以将它与可绘制资源进行比较,例如:

if(imageview.getDrawable()==getResources().getDrawable(R.drawable.fund1) || imageview.getDrawable()==getResources().getDrawable(R.drawable.fund2) || imageview.getDrawable()==getResources().getDrawable(R.drawable.fund3)){
    //do work here
}

当然,您可以使用更好的方式进行比较,我只是举了一个例子。希望它有所帮助!