Android:如何以编程方式将视图的longclickable设置为true,同时保持其clickable为false?

时间:2016-04-22 04:16:36

标签: android clickable onlongclicklistener

我有一个最初在xml中开始的视图:

<ImageButton
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:id="@+id/likeBtn"
            android:src="@drawable/like"
            android:onClick="like"
            android:longClickable="true"
            android:clickable="true" />

我在mainActivity的OnCreate中已经有一个setOnLongClickListenter,如下所示:

likeBtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showExtraLike(v);
                return true;
            }
        });

我的问题是:

当我运行像这样的代码时

likeBtn.setClickable(false);
likeBtn.setLongClickable(false);  

/* some other code here */

likeBtn.setLongClickable(true);

我发现视图也可以点击!!

我需要它 LongClickable和 NOT 可以点击一段时间,因为我会在代码中的几行之后再次启用它们。

备注:

  • 禁用它们并再次启用它们可以正常工作。
  • 仅禁用LongClick并再次启用它可以正常工作。
  • 我唯一的问题是,当longClickable为true时,将clickable设置为false似乎无效!

4 个答案:

答案 0 :(得分:1)

只需像这样设置onlongclicklistener:

likeBtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                  //do something
                  return true;
            }
        });

希望它可以帮到你

答案 1 :(得分:0)

首先启用longclickable并添加set onlongclicklistener

var Example = React.createClass({
    scrollUp: function () {
        var doc = document.documentElement;
        var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);

        if (top > 0) {
            window.scrollTo(0, top - 15)
            setTimeout(this.scrollUp, 10)
        }
    },
    render: function () {
        return (<div><h1>Page Header</h1>
            <div ref="container" className="container">
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
                    et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
                    aliquip ex ea commodo consequat. Duis aute irure dolor
                    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
                    occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                </p>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
                    et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
                    aliquip ex ea commodo consequat. Duis aute irure dolor
                    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
                    occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                </p>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
                    et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
                    aliquip ex ea commodo consequat. Duis aute irure dolor
                    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
                    occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                </p>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
                    et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
                    aliquip ex ea commodo consequat. Duis aute irure dolor
                    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
                    occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                </p>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
                    et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
                    aliquip ex ea commodo consequat. Duis aute irure dolor
                    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
                    occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                </p>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
                    et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
                    aliquip ex ea commodo consequat. Duis aute irure dolor
                    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
                    occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                </p>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
                    et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
                    aliquip ex ea commodo consequat. Duis aute irure dolor
                    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
                    occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                </p>

                <a className="move-top" onClick={this.scrollUp}>Up</a>
            </div>
        </div>);
    }
});

答案 2 :(得分:0)

您的问题是您从xml调用click方法进行活动。删除该行,如果要启用或禁用点击,最初将两者都设置为false。用

替换xml代码
<ImageButton
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:id="@+id/likeBtn"
    android:src="@drawable/common_ic_googleplayservices"
    android:longClickable="false"
    android:clickable="false" />

现在在您的活动中覆盖OnClick和onLongClick方法。

 findViewById(R.id.likeBtn).setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(MainActivity.this,"like long clicked",Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    findViewById(R.id.likeBtn).setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(MainActivity.this,"like long clicked",Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    findViewById(R.id.likeBtn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
     // Call LOGIN();
            Toast.makeText(MainActivity.this,"like  clicked",Toast.LENGTH_SHORT).show();
        }
    });

现在您可以在活动的任何位置启用和禁用它。希望这可以帮助。 :)

答案 3 :(得分:0)

我已经达成了解决方法。

XML中的clickable和longClickable都设置为true:

<ImageButton
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:id="@+id/likeBtn"
                android:src="@drawable/like"
                android:clickable="true"
                android:longClickable="true"/>

但是我从mainActivity中删除了所有的setClickable和setLongClickable,并在onCreate中进行了以下操作,以使它们在某些情况下不执行任何操作:

// setting click listener for question button
        likeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (var == 1 || var == 3 || var == 4);
                   // do nothing

                else
                    like(v);
            }
        });

        // setting longclick listener for question button
        likeBtn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (var == 1 || var == 2 || var == 4)
                   likeBtn.setHapticFeedbackEnabled(false);

            else{
                likeBtn.setHapticFeedbackEnabled(true);
                showExtraLike(v);
            }

                return true;
            }
        });

它有效!

但是,当然,当longClick充当禁用时,它仍在振动设备......这就是我添加setHapticFeedbackEnabled的原因。