android按钮setPressed在onClick之后 [英] android button setPressed after onClick

查看:28
本文介绍了android按钮setPressed在onClick之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我注意到可以通过 Compatibility 包将 Fragments 集成到旧的 API 级别中,但这对于这个问题并不是很重要.:)

yesterday I noticed the possibility to integrate Fragments in older API Levels through the Compatibility package, but thats not really essential for the question. :)

我有一个带有 OnClickListener 的按钮

I have a Button with an OnClickListener

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        doSomething();
        button.setPressed(true);
    }
});

由于实际点击,显示为按下,释放点击后,按钮状态未按下并保持不变.

Because of the actual clicking, it is shown as pressed and after releasing the click, the button state is not pressed and stays that way.

有没有一种简单的方法可以让按钮释放后保持按下状态?我能想到的第一件事是某种计时器,但这似乎不合理.

Is there a simple way that keeps the button state pressed after releasing? First thing I can think of would be some sort of timer, but that seems unreasonable.

推荐答案

您可以将按钮状态保存在 drawable 文件夹下的 xml 文件中,然后用作按钮的背景.例如:

You can keep the button states in xml file under drawable folder, then used as background for button. For example:

 android:background="@drawable/buttonstate"

buttonstate.xml

buttonstate.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/back_focused" />
    <item android:drawable="@drawable/back" /> <!-- default -->
</selector>

这篇关于android按钮setPressed在onClick之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆