我在“活动”中有如下文字视图:1/9(holeCounter / holeNm)。我有2个在ImageView中操纵holeCounter(添加或减少数量)的图像视图,左箭头图像表示减少,右箭头图像表示增加数字。现在,当我的HoleCounter == holeNm(9/9)时,此向右箭头图像应变成另一张图像。

注意:这些箭头图像是在“活动” XML文件中创建的,例如:

android:background="@drawable/ic_arrow_left"
android:background="@drawable/ic_arrow_right"


我现在所拥有的:

private void setButtons() {
        mBackArrow = findViewById(R.id.previous);
        mForwardArrow = findViewById(R.id.next);

        mBackArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holeCounter > 1) {
                    Intent intent = getIntent();
                    mHoleNm = findViewById(R.id.gameHoleNumber);

                    holeCounter--;
                    mHoleNm.setText(holeCounter + "/" + intent.getStringExtra("HOLESNM"));
                }
            }
        });

        mForwardArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = getIntent();
                int holesNm = Integer.parseInt(intent.getStringExtra("HOLESNM"));

                if (holeCounter == (holesNm - 1)) {
                    mForwardArrow.setImageResource(R.drawable.ic_finish);
                }

                if (holeCounter < holesNm) {
                    mHoleNm = findViewById(R.id.gameHoleNumber);
                    holeCounter++;
                    mHoleNm.setText(holeCounter + "/" + intent.getStringExtra("HOLESNM"));
                }
            }
        });
    }


现在,它在该右箭头上方添加了新的drawable,但是我希望该右箭头在该新图像下方消失。同样在此之后,如果用户单击向左箭头,现在单击holeCounter!= holeNm,则其他图像应返回到向右箭头图像。

最佳答案

您必须使用android:src代替android:background。替换下面的代码行

android:background="@drawable/ic_arrow_left"
android:background="@drawable/ic_arrow_right"




android:src="@drawable/ic_arrow_left"
android:src="@drawable/ic_arrow_right"

07-28 08:12