安卓系统中,按钮没有按下的外观

3

我刚开始接触Android开发。我创建了一个非常简单的按钮,如下:

<Button
    android:id="@+id/howItWorksButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="58dp"
    android:text="@string/how_it_works_button_title" />

在代码中,我设置了它的背景颜色:

howItWorksButton.setBackgroundColor(Color.RED);

这是 onClickListener:

howItWorksButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View view)
        {
            Intent intent = new Intent(MainMenuActivity.this, HowItWorksActivity.class);
            MainMenuActivity.this.startActivity(intent);
        }
    });

问题在于我点击它时,它看起来并没有被按下,它仍然保持原样,没有任何变化。它的onClick方法运行良好,功能也没问题,但我希望它在某种程度上看起来已经发生了变化,就像所有按钮在被按下时一样。


@Numair 我已经完成了。 - Andrey Chernukha
1
@Numair,你错了,你不需要实现任何东西就可以看到按下的效果。问题一定是出在其他地方。Andrey,请确保你的代码中没有做奇怪的事情。 - akaya
你是否应用了任何自定义主题? - Padma Kumar
@kaya,请查看我的编辑答案。我发布了OnClickListener。 - Andrey Chernukha
好的,发布答案。 - akaya
显示剩余7条评论
4个回答

8

与iOS不同,如果您手动设置颜色,Android不会自动显示按下状态。以下是建议的解决方法。

在您的values文件夹中放置一个colors.xml文件夹。其内容应如下所示。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffffff</color>
    <color name="darkwhite">#ffeeeeee</color>
    <color name="transparent">#00ffffff</color>
    <color name="semitransparent">#88000000</color>
    <color name="orange">#ffff8800</color>
    <color name="light_orange">#ffe27d18</color>
    <color name="light_blue">#ff00a2e8</color>
    <color name="black">#ff000000</color>
    <color name="menuButtonColor">#ffea8e44</color>
</resources>

请在drawable文件夹中放置一个选择器文件,例如:
my_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/light_orange" android:state_pressed="true"/>
    <item android:drawable="@color/orange"/>
</selector>

像这样使用您的选择器

<Button
    android:id="@+id/howItWorksButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="58dp"
    android:text="how_it_works_button_title"
    android:background="@drawable/my_button_selector" />

2

尝试这样做:

使用选择器

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/login_btn" android:state_pressed="false"/>
<item android:drawable="@drawable/login_btn_focus" android:state_pressed="true"/>

</selector>

0
在两个状态 pressed 和其他 state 中都写入 android:state_pressed=" "当按钮被按下时
<item android:state_pressed="true">
    <shape>
        <solid android:color="@color/green" />
        <corners android:radius="@dimen/btn_corner_rds" />
    </shape>
</item>

当按钮未被按下时
<item android:state_pressed="false">
    <shape>
        <solid android:color="@android:color/transparent" />
        <stroke android:width="@dimen/stroke_width_btn"  />
        <corners android:radius="@dimen/btn_corner_rds" />
    </shape>
</item>

0

仅设置颜色是不正确的。它将用于每个状态(默认,按下,聚焦等)。因此实际上,您对按钮所做的任何更改都不会改变。 - zubke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接