按钮选择器pressed_state不起作用



我正在尝试为按下按钮使用不同的样式,但它不起作用。(默认样式有效。这是完整的代码。请你看看问题。

活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="???"
        android:background="@drawable/button_generic"/>
</RelativeLayout>

可绘制

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"> <!--Android ignores this :-/ -->
        <shape>
            <solid android:color="#ff0000" />
            <stroke android:width="2dp" android:color="#adadad" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#e8e8e8" />
            <stroke android:width="1px" android:color="#adadad" />
        </shape>
    </item>
</selector>

活动

public class TActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.t);
 }
}

尝试:

  • 将以下 2 个属性添加到 XML 中的Button

android:clickable="true" android:focusable="true"

  • 或在Activity中以编程方式将View.OnClickListener添加到Button

*我怎么理解,您想在单击按钮时进行一些更改,好吗?在您的代码中,当用户单击按钮时,它会将背景颜色更改为红色。

如果是问题,在你的代码中你只是有问题*

android:text="???" 

将其转换为

android:text="sometext"

它将 100% 工作

点击前

点击后

放了两张图片,它如何与您的代码一起工作,我只是将"???"更改为"AAA"。

最新更新