android - 长按监听器以停用 View

标签 android view selector clickable onlongclicklistener

我有一些以编程方式激活和停用的 View (ImageView)。我已经实现了这个方法来激活/停用 View 的状态:

/**
     * Change the state of the bottom PopUp menu of buttons.
     * 
     * @param state
     *            The state in which to change the menu. <b>true</b> for active,
     *            <b>false</b>otherwise.
     * @param includePaste
     *            If to include the paste button into the state change of the
     *            menu buttons.
     */
    private void setPopUpBottomState(final boolean state,
            final boolean includePaste) {
        if (null != popup_download) {
            popup_download
                    .setImageResource(state ? R.drawable.ic_bottom_menu_download
                            : R.drawable.ic_bottom_menu_download_inactive);
            popup_download.setClickable(state);
            popup_download.setEnabled(state);
        }
        if (null != popup_share) {
            popup_share
                    .setImageResource(state ? R.drawable.ic_bottom_menu_share
                            : R.drawable.ic_bottom_menu_share_inactive);
            popup_share.setClickable(state);
            popup_share.setEnabled(state);
        }
        if (null != popup_copy) {
            popup_copy.setImageResource(state ? R.drawable.ic_bottom_menu_copy
                    : R.drawable.ic_bottom_menu_copy_inactive);
            popup_copy.setClickable(state);
            popup_copy.setEnabled(state);
        }
        if (null != popup_cut) {
            popup_cut.setImageResource(state ? R.drawable.ic_bottom_menu_cut
                    : R.drawable.ic_bottom_menu_cut_inactive);
            popup_cut.setClickable(state);
            popup_cut.setEnabled(state);
        }
        if (null != popup_rename) {
            popup_rename
                    .setImageResource(state ? R.drawable.ic_bottom_menu_rename
                            : R.drawable.ic_bottom_menu_rename_inactive);
            popup_rename.setClickable(state);
            popup_rename.setEnabled(state);
        }
        if (null != popup_delete) {
            popup_delete
                    .setImageResource(state ? R.drawable.ic_bottom_menu_trash
                            : R.drawable.ic_bottom_menu_trash_inactive);
            popup_delete.setClickable(state);
            popup_delete.setEnabled(state);
        }
        if (includePaste && null != popup_paste) {
            popup_paste
                    .setImageResource(state ? R.drawable.ic_bottom_menu_paste
                            : R.drawable.ic_bottom_menu_paste_inactive);
            popup_paste.setClickable(state);
            popup_paste.setEnabled(state);
        }
    }

一切正常,但现在我想在 View 处于 Activity 状态时为它们添加一个长按监听器。这是我在 onCreate() 中调用的方法来设置长按监听器:

/**
 * Set the long click listener for each ImageView on the bottom popup menu
 * options. <br />
 * Will be active only when the options are active.
 */
private void setPopUpBottomLongClickListener() {
    // long click listener for hints
    popup_download.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (popup_download.isClickable() && popup_download.isEnabled()) {
                Toast.makeText(
                        FileManagerActivity.this,
                        getResources().getString(
                                R.string.bottom_menu_download),
                        Toast.LENGTH_LONG).show();
            }
            // The callback consumed the click
            return true;
        }
    });
    popup_share.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (popup_share.isClickable() && popup_share.isEnabled()) {
                Toast.makeText(
                        FileManagerActivity.this,
                        getResources()
                                .getString(R.string.bottom_menu_share),
                        Toast.LENGTH_LONG).show();
            }
            // The callback consumed the click
            return true;
        }
    });
    popup_copy.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (popup_copy.isClickable() && popup_copy.isEnabled()) {
                Toast.makeText(
                        FileManagerActivity.this,
                        getResources().getString(R.string.bottom_menu_copy),
                        Toast.LENGTH_LONG).show();
            }
            // The callback consumed the click
            return true;
        }
    });
    popup_cut.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (popup_cut.isClickable() && popup_cut.isEnabled()) {
                Toast.makeText(FileManagerActivity.this,
                        getResources().getString(R.string.bottom_menu_cut),
                        Toast.LENGTH_LONG).show();
            }
            // The callback consumed the click
            return true;
        }
    });
    popup_rename.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (popup_rename.isClickable() && popup_rename.isEnabled()) {
                Toast.makeText(
                        FileManagerActivity.this,
                        getResources().getString(
                                R.string.bottom_menu_rename),
                        Toast.LENGTH_LONG).show();
            }
            // The callback consumed the click
            return true;
        }
    });
    popup_delete.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (popup_delete.isClickable() && popup_delete.isEnabled()) {
                Toast.makeText(
                        FileManagerActivity.this,
                        getResources().getString(
                                R.string.bottom_menu_delete),
                        Toast.LENGTH_LONG).show();
            }
            // The callback consumed the click
            return true;
        }
    });
    popup_paste.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (popup_paste.isClickable() && popup_download.isEnabled()) {
                Toast.makeText(
                        FileManagerActivity.this,
                        getResources()
                                .getString(R.string.bottom_menu_paste),
                        Toast.LENGTH_LONG).show();
            }
            // The callback consumed the click
            return true;
        }
    });
}

出现的问题是,即使项目没有被激活(第一次只出现选择器),项目也是可点击的,但下一次(激活然后停用)项目也是可点击的。

我还有一个选择器设置为每个 ImageView 的背景:

<!-- Selector for bottom menu buttons -->
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@android:color/transparent" />

<item android:state_pressed="true" 
    android:drawable="@color/ic_bottom_menu_selector" />

<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@color/ic_bottom_menu_selector" />

我在这里做错了什么?

最佳答案

这个线程处理同样的问题Button.setClickable(false) is not working

我通常使用函数 view.setEnabled(false); 来做这样的事情。

但在我链接到的线程中,他们也建议使用

view.setFocusableInTouchMode(false);

view.setClickable(false);
view.setFocusable(false);

关于android - 长按监听器以停用 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21112260/

相关文章:

android - margin 仍然存在于替换的 fragment 中

Grails - 将数据从 View 传递到 Controller

javascript - 如何干净地访问 jquery 对象的直接后代?

css - 如何使用 selenium webdriver 遍历并单击“下一步”和“取消”按钮(在弹出窗口上)

android - (Android) 保持服务运行,即使应用程序被强制停止

android - 使 AsyncTask 静态(有问题,提供代码)

android - 不同设备上不同的 Box2D 主体速度

django - 从另一个 View 调用服务器上的API

entity-framework-4 - Entity Framework 4 - 更新/插入 View

表行 id 的 Jquery 选择器