【问题标题】:Button state drawable pressed triggering multiple buttons按钮状态drawable按下触发多个按钮
【发布时间】:2019-12-29 16:16:36
【问题描述】:

将一些按钮的背景设置为可绘制的 custom_button.xml。选择器应该在按下、禁用和默认时更改可绘制对象。出于某种原因,当我按下视图中的任何按钮时,布局中的最后一个按钮也将其可绘制设置为 rounded_corner_pressed。这一切都是通过选择器完成的。我试过添加 state_focus 无济于事。有任何想法吗?

编辑试图让问题更清楚

custom_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true" f
        android:drawable="@drawable/rounded_corner_pressed" />
    <item
        android:state_enabled="false"
        android:drawable="@drawable/rounded_corner_disabled" />
    <item
        android:drawable="@drawable/rounded_corner" />
</selector>

rounded_corner.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white"/>
    <corners android:topLeftRadius="8dp"
        android:topRightRadius="8dp"
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"/>
    <stroke
        android:width="1dp"
        android:color="@color/background_color" />
    <padding android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>

rounded_corner_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/holo_green_light"/>
    <corners android:topLeftRadius="8dp"
        android:topRightRadius="8dp"
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"/>
    <stroke
        android:width="1dp"
        android:color="@color/background_color" />
    <padding android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>

布局它在

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="10sp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/questionView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corner"
        android:gravity="center_horizontal|center_vertical"
        android:minHeight="85dp"
        android:text="Question"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black"
        android:textStyle="bold" />

    <Button
        android:id="@+id/answerOne"
        android:layout_width="match_parent"
        android:layout_height="65dip"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/custom_button"
        android:layout_below="@id/questionView"
        android:text="Answer 1" />

    <Button
        android:id="@+id/answerTwo"
        android:layout_width="match_parent"
        android:layout_height="65dip"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/custom_button"
        android:layout_below="@id/answerOne"
        android:text="Answer 2" />

    <Button
        android:id="@+id/answerThree"
        android:layout_width="match_parent"
        android:layout_height="65dip"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/custom_button"
        android:layout_below="@id/answerTwo"
        android:text="Answer 3" />

    <Button
        android:id="@+id/answerFour"
        android:layout_width="match_parent"
        android:layout_height="65dip"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/custom_button"
        android:layout_below="@id/answerThree"
        android:text="Answer 4" />

    <ProgressBar
        android:id="@+id/retrievingProgress"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:indeterminate="true"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

【问题讨论】:

    标签: android android-layout material-design android-drawable material-components-android


    【解决方案1】:

    您可以简化布局。 对于您的按钮,只需使用 Material Components library 中的 MaterialButton

    类似:

    <com.google.android.material.button.MaterialButton
        android:text="BUTTON"
        app:cornerRadius="8dp"
        app:strokeWidth="1dp"
        app:strokeColor="@color/myColor"
        android:backgroundTint="@color/myselector"
        ../>
    

    对于 TextView,您可以使用 TextInputLayout

    类似:

    <com.google.android.material.textfield.TextInputLayout
        app:hintEnabled="false"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        app:boxCornerRadiusBottomStart="8dp"
        app:boxCornerRadiusBottomEnd="8dp"
        app:boxCornerRadiusTopEnd="8dp"
        app:boxCornerRadiusTopStart="8dp"
        ...>
    
        <com.google.android.material.textfield.TextInputEditText
            android:text="Text"
            .../>
    
    </com.google.android.material.textfield.TextInputLayout>
    

    【讨论】:

      【解决方案2】:

      试试

      onlick{
      lastbuttonName.performClick();
      }
      

      【讨论】:

      • 我试图理解你的问题,我理解你想通过第一个按钮点击触发最后一个按钮。
      【解决方案3】:

      好吧,我找到了答案,我对此并不满意,但它确实解决了问题。单击其中一个按钮后,我将背景更改为不正确的可绘制对象(类似于圆角,只是背景颜色不同)几秒钟,然后将其设置回来。

      Drawable defaultDrawable = ContextCompat.getDrawable(this, R.drawable.custom_button);
      answerOne.setBackground(defaultDrawable);
      answerTwo.setBackground(defaultDrawable);
      answerThree.setBackground(defaultDrawable);
      answerFour.setBackground(defaultDrawable);
      

      这一定会导致引用问题,因为现在每当我单击按钮 1、2 或 3 时,按钮 4 的状态也会随之更改。为了解决这个问题,我只是破解了它,并像这样为每个按钮添加了一个独特的可绘制对象。

      Drawable defaultDrawable = ContextCompat.getDrawable(this, R.drawable.custom_button);
      Drawable defaultDrawable1 = ContextCompat.getDrawable(this, R.drawable.custom_button);
      Drawable defaultDrawable2 = ContextCompat.getDrawable(this, R.drawable.custom_button);
      Drawable defaultDrawable3 = ContextCompat.getDrawable(this, R.drawable.custom_button);
      answerOne.setBackground(defaultDrawable);
      answerTwo.setBackground(defaultDrawable1);
      answerThree.setBackground(defaultDrawable2);
      answerFour.setBackground(defaultDrawable3);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多