【问题标题】:How to create a history of pressing ImageButton如何创建按下 ImageButton 的历史记录
【发布时间】:2015-05-24 13:27:21
【问题描述】:

我是一名新手开发人员。请帮助创建代码以显示按下 ImageButton 的历史记录(在 Android 上)。我有 3 个 ImageButton(IB1、IB2、IB3)和 5 个空的 ImageView(IV1、IV2、IV3、IV4、IV5)。

当用户第一次触摸三个 ImageButton 之一(例如 IB2)时,ImageView (IV1) 会显示来自 IB2 的按钮图像。

当用户第二次单击并再次触摸三个按钮中的一个(例如这次是 IB1)时,第一个按下按钮的图像(来自 IB2 的图像)从 IV1 移动到 IV2,IV1 从按钮获取图像第二次点击——来自 IB1。等等无限次,但历史只显示最后 5 次点击。这是我的代码的开头。在此先感谢大家。

public class MainActivity extends Activity implements View.OnClickListener {

    ImageView IV1, IV2, IV3, IV4, IV5;
    ImageButton IB1, IB2, IB3;

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

        IB1 = (ImageButton) findViewById(R.id.IB_first);
        IB2 = (ImageButton) findViewById(R.id.IB_second);
        IB3 = (ImageButton) findViewById(R.id.IB_third);

        IV1 = (ImageView)findViewById(R.id.ImageView1);
        IV2 = (ImageView)findViewById(R.id.ImageView2);
        IV3 = (ImageView)findViewById(R.id.ImageView3);
        IV4 = (ImageView)findViewById(R.id.ImageView4);
        IV5 = (ImageView)findViewById(R.id.ImageView5);

        IB1.setOnClickListener(this);
        IB2.setOnClickListener(this);
        IB3.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.IB1:
                IV1.setVisibility(View.VISIBLE);
                IV1.setImageResource(R.drawable.first_image);
                break;
            case R.id.IB2:
                IV1.setVisibility(View.VISIBLE);
                IV1.setImageResource(R.drawable.second_image);
                break;
            case R.id.IB3:
                IV1.setVisibility(View.VISIBLE);
                IV1.setImageResource(R.drawable.third_image);
                break;
        }
    }
}

【问题讨论】:

    标签: android imageview imagebutton


    【解决方案1】:

    在您的 clicklistner 中,您应该将可绘制对象传递给其他 imageViews
    例如:

    IV5.setImageDrawable(IV4.getDrawable());
    IV4.setImageDrawable(IV3.getDrawable());
    IV3.setImageDrawable(IV2.getDrawable());
    IV2.setImageDrawable(IV1.getDrawable());  
    // set your IV1 resource  
    

    快乐编码 ;)

    【讨论】:

    • 好吧@Valery,您应该投票并标记为正确,以便帮助其他人找到正确的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多