Android 图像按钮行为异常

发布于 2024-12-09 08:11:53 字数 1824 浏览 1 评论 0原文

我正在为 Android 平板电脑构建一个应用程序框架,但我遇到了一些 ImageButtons 的奇怪问题。这些按钮(下一个和上一个)用于帮助浏览作为框架一部分的“内容页面”,并使用自定义背景可绘制对象。

问题是,每当我单击“上一个”按钮时,“下一个”按钮就会突出显示,而“上一个”按钮则不会突出显示。起初我以为这是一个简单的链接或@+id/问题,但是执行了“上一个”按钮的操作,而不是“下一个”按钮的操作。

以下是一些相关代码:

在 onCreate() 方法中:

nextButton = (ImageButton) findViewById(R.id.nextButton);
prevButton = (ImageButton) findViewById(R.id.prevButton);
nextButton.setOnClickListener(this);
prevButton.setOnClickListener(this);

onClick 方法:

public void onClick(View v) {
    if(_cpp == null){
        finish();
    }else if (v == nextButton) {
        if (!_cpp.nextRes.equalsIgnoreCase("")) {
            // load next page
            _tempRes = _cpp.nextRes;
            //start transition out animations
            fadePageOut();
        } else {
            finish();
        }
    } else if (v == prevButton) {
        if (!_cpp.prevRes.equalsIgnoreCase("")) {
            // load previous page
            _tempRes = _cpp.prevRes;
            //start transition out animations
            fadePageOut();
        }
    }
}

我的可绘制 XML:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable= "@drawable/prev_arrowhigh" />
<item android:state_pressed="true" android:state_enabled="true"
    android:drawable="@drawable/prev_arrowhigh" />
<item android:state_focused="true" android:state_enabled="true"
    android:drawable="@drawable/prev_arrow" />
<item android:state_enabled="true" android:drawable="@drawable/prev_arrow" />
</selector>

我已经调试并寻找答案两天了,但没有运气。有趣的是,它曾经工作得很好......但即使恢复到我的项目的以前版本似乎也没有任何帮助。

有没有人以前遇到过这个问题或者知道这可能是什么原因?

I'm building an app framework for Android tablets, and I am running into a strange issue with some ImageButtons. These buttons (Next & Prev) are used to help navigate through "content pages" as part of the framework, and are using custom background drawables.

The issue is that whenever I click on the Prev button, the Next button highlights while the the Prev button does not. At first I thought it was a simple linking or @+id/ issue, but the Prev button's action is performed, not the Next button's.

Here is some relevant code:

In onCreate() method:

nextButton = (ImageButton) findViewById(R.id.nextButton);
prevButton = (ImageButton) findViewById(R.id.prevButton);
nextButton.setOnClickListener(this);
prevButton.setOnClickListener(this);

onClick method:

public void onClick(View v) {
    if(_cpp == null){
        finish();
    }else if (v == nextButton) {
        if (!_cpp.nextRes.equalsIgnoreCase("")) {
            // load next page
            _tempRes = _cpp.nextRes;
            //start transition out animations
            fadePageOut();
        } else {
            finish();
        }
    } else if (v == prevButton) {
        if (!_cpp.prevRes.equalsIgnoreCase("")) {
            // load previous page
            _tempRes = _cpp.prevRes;
            //start transition out animations
            fadePageOut();
        }
    }
}

My drawable XML:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable= "@drawable/prev_arrowhigh" />
<item android:state_pressed="true" android:state_enabled="true"
    android:drawable="@drawable/prev_arrowhigh" />
<item android:state_focused="true" android:state_enabled="true"
    android:drawable="@drawable/prev_arrow" />
<item android:state_enabled="true" android:drawable="@drawable/prev_arrow" />
</selector>

I've been debugging and searching for answer for two days now, with no luck. The funny thing is it used to work just fine... but even reverting to a previous version of my project didn't seem to help at all.

Has anyone encountered this issue before or have any idea what could possibly be the cause of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很OK 2024-12-16 08:11:53

您可以尝试的解决方案是在视图上设置 onClick 属性。解决方案可能如下所示:

onCreate 方法:

nextButton = (ImageButton) findViewById(R.id.nextButton);
prevButton = (ImageButton) findViewById(R.id.prevButton);

Activity 的新方法:

public void nextButton(View v)
{
    if(_cpp == null || _cpp.nextRes.equalsIgnoreCase(""))
    {
        finish();
        return;
    }

    // load next page
    _tempRes = _cpp.nextRes;
    //start transition out animations
    fadePageOut();
}

public void prevButton(View v)
{
    if(_cpp == null)
    {
        finish();
        return;
    }

    if (!_cpp.prevRes.equalsIgnoreCase(""))
    {
        // load next page
        _tempRes = _cpp.nextRes;
        //start transition out animations
        fadePageOut();
    }
}

并添加:
android:onClick="nextButton"
android:onClick="prevButton"

添加到 xml 文件中的按钮

A solution you can try is to set the onClick attribute on your view. The solution might look like this:

onCreate method:

nextButton = (ImageButton) findViewById(R.id.nextButton);
prevButton = (ImageButton) findViewById(R.id.prevButton);

new methods for the Activity:

public void nextButton(View v)
{
    if(_cpp == null || _cpp.nextRes.equalsIgnoreCase(""))
    {
        finish();
        return;
    }

    // load next page
    _tempRes = _cpp.nextRes;
    //start transition out animations
    fadePageOut();
}

public void prevButton(View v)
{
    if(_cpp == null)
    {
        finish();
        return;
    }

    if (!_cpp.prevRes.equalsIgnoreCase(""))
    {
        // load next page
        _tempRes = _cpp.nextRes;
        //start transition out animations
        fadePageOut();
    }
}

and add:
android:onClick="nextButton"
android:onClick="prevButton"

to your your buttons in the xml file

蹲在坟头点根烟 2024-12-16 08:11:53

解决了!愚蠢,愚蠢的我...

下一个按钮显示为选定状态,因为在我的代码(未发布的部分)中,在我的 fadePageOut() 方法期间禁用了该按钮(请参阅发布的代码)。状态绘制只是使用了错误的图像来表示启用/禁用状态。

这:

<item android:state_enabled="false" android:drawable="@drawable/prev_arrowhigh"/>

应该是:

<item android:state_enabled="false" android:drawable="@drawable/prev_arrow"/>

因为图像不正确而浪费了两天......
*叹*

Solved! stupid, stupid me...

Next button was displaying as selected because in my code (part not posted) disables the button during my fadePageOut() method (see posted code). The state-drawable was simply using the wrong images for enabled/disabled states.

This:

<item android:state_enabled="false" android:drawable="@drawable/prev_arrowhigh"/>

Should have been:

<item android:state_enabled="false" android:drawable="@drawable/prev_arrow"/>

two days wasted because of a incorrect image...
*sigh*

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文