简体   繁体   English

如何使按钮不可点击但可触摸?

[英]How to make a button unclickable but Touchable?

I have a button, when I click it it makes a scale animation, and also I can drag it with onTouch event.我有一个按钮,当我点击它时它会产生一个缩放动画,我也可以用 onTouch 事件拖动它。

I want to make it unclickable after first touch and this code does.我想让它在第一次触摸后无法点击,而这段代码可以。 Here OnClick在这里点击

    btnScale.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {

            v.startAnimation(animScale);
            btnScale.setClickable(false);

        }});

Also this code help me to drag it.这段代码也帮助我拖动它。 Here OnTouch在这里 OnTouch

 btnScale.setOnTouchListener(new Button.OnTouchListener(){

            public boolean onTouch(View v, MotionEvent me) {
                if (me.getAction() == MotionEvent.ACTION_DOWN) {
                    oldXvalue = me.getX();
                    oldYvalue = me.getY();
                } else if (me.getAction() == MotionEvent.ACTION_MOVE) {

                    //RelativeLayout rl = (RelativeLayout) findViewById(R.id.background);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());

                    params.leftMargin = (int) (me.getRawX() - (v.getWidth() / 2));
                    params.topMargin = (int) (me.getRawY() - (v.getHeight()));

                    //LayoutParams params = new LayoutParams(v.getHeight(), (int) (me.getRawX() - (v.getWidth() / 2)), (int) (me.getRawY() - (v.getHeight())));
                    v.setLayoutParams(params);
                }
                return false;
            }});

However I still want to drag it after I click it.但是,我仍然想在单击它后拖动它。 However when I make it unclickable it cannot be dragable since it is unTouchable too但是,当我使它不可点击时,它也无法拖动,因为它也是不可触摸的

EDIT:编辑:

When I use if else state on onClick as boolean variable, I still able to click the button and a ugly square appears in my circular button.当我在 onClick 上使用 if else 状态作为布尔变量时,我仍然可以单击该按钮,并且圆形按钮中会出现一个丑陋的正方形。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#61d5ee">
    <item>
        <shape android:shape="oval">
            <solid android:color="#61d5ee"/>
           <corners android:radius="5dp"  />
           <stroke android:width="4px" android:color="#cdf3fb" />
        </shape>
    </item>
</ripple>

What to do?该怎么办?

Create a boolean to say if it is enabled or not, change manually the state in your onClick and change the button's design too.创建一个布尔值来说明它是否已启用,手动更改 onClick 中的状态并更改按钮的设计。 In your Onclick use something like:在你的 Onclick 中使用类似的东西:

if(enabled){
   do something
}else{
    //do nothing
}

You don't need the else, it's just an example.你不需要其他,这只是一个例子。 Then... he is touchable enabled and disabled.然后……他是可触摸的启用和禁用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM