简体   繁体   中英

How to change two button images when clicking one in Android?

I am creating an Android, java-based Minesweeper knock-off for a college project.

Right now, I can't find how to set two buttons to pressed state and make them both revert to not-pressed again after releasing the click (more specifically the "emoticon button", when pressing one of the 1, 2, blank or mine buttons). That's what I mean: Screenshot .

I tried setting the Smile button to pressed state with: buttonEmotikon.setPressed(!buttonEmotikon.isActivated()); and <item android:state_pressed="true" android:drawable="@drawable/wow"/> in the xml, but it doesn't change back to primary image after the onClick instruction.

This is the full xml file for smile button:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/wow"/>
    <item android:state_activated="true">
        <bitmap android:src="@drawable/sad"/>
    </item>
    <item android:state_first="true" android:drawable="@drawable/smile2"/>
    <item android:drawable="@drawable/smile2"/>
</selector>

This is an excerpt of the layout with smile button and generic grey button (which is the same as the rest):

`<ImageButton
            android:layout_width="47dp"
            android:layout_height="47dp" app:srcCompat="@drawable/block"
            android:id="@+id/imageButton25"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="52dp"
            android:layout_marginTop="4dp" app:layout_constraintTop_toBottomOf="@+id/imageButton20"
            android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.0" app:layout_constraintStart_toEndOf="@+id/imageButton24"
            android:layout_marginStart="3dp" app:layout_constraintHorizontal_bias="0.0"
            android:src="@drawable/field_bomb" tools:ignore="RtlCompat"/>
    <ImageButton
            android:layout_width="49dp"
            android:layout_height="49dp" app:srcCompat="@drawable/smile2"
            android:id="@+id/buttonEmotikon" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
            android:layout_marginTop="60dp" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0.488" app:layout_constraintBottom_toBottomOf="@+id/imageView"
            app:layout_constraintVertical_bias="0.019" android:src="@drawable/emotikon" tools:ignore="RtlCompat"/>`

My onClick method looks like:

`@Override
    public void onClick(View v){
        Intent intent;
        v.setActivated(!v.isActivated());
        v.setEnabled(false);
        switch (v.getId()) {
            case R.id.buttonEmotikon:

` and there are cases with different button ids. All clickable objects are ImageButtons.

Thanks in advance.

您应该使用选择器作为 ToggleButton 的背景,并将您的图像置于正确的状态,如本答案

To change the image to the boolean value of isActivated, add the following code:

<item android:state_activated="false">
    <bitmap android:src="@drawable/smile2"/>
</item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM