android - 按钮选择器 pressed_state 不起作用

标签 android

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

Activity

<?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>

Activity

public class TActivity extends Activity {

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

}

最佳答案

试试:

  • 将这两个属性添加到 xml 中的 Button:

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

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

关于android - 按钮选择器 pressed_state 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49960308/

相关文章:

android - 如何将 Kotlin 流程拆分为 2 个流程?

android - Android 应用程序中未调用 Facebook 登录回调

android - 如何检查应用是否违反了 Google 的假冒政策

android - 在 CursorAdapter 中加入多条记录

android - 如何将android WebView 放入AlertDialog 中?

android - 如何在单击按钮时从 ListView 中动态删除项目?

java - android studio 按钮不起作用

android - 我怎样才能格式化 sdcard android 应用程序

android - 使用卫星与 Android 手机进行双向通信

android - 最近完成的 Activity 从(推送通知)Android 中的最近任务重新打开