简体   繁体   English

如何在Android上实现对按钮的手势?

[英]how to implement gesture to a button on android?

I have a button (button1) and two sound (sound1, sound2), I want to implement a gesture to this button. 我有一个按钮(button1)和两个声音(sound1,sound2),我想对该按钮实现一个手势。 if Button1 is clicked, then the sound is sound1 but if Button1 is touched by the gesture, the sound is sound2. 如果单击Button1,则声音为sound1,但是如果手势触摸Button1,则声音为sound2。 to handle the sound, I use a SoundManager class. 为了处理声音,我使用了SoundManager类。 to handle touch I use multitouch class. 处理触摸我使用多点触摸类。

public class MyActivity extends MultiTouch {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity);

    SoundManager.getInstance();
    SoundManager.initSounds(this);
    SoundManager.loadSounds();

    Button Button1 = (Button)findViewById(R.id.button1);
    Button1.setOnTouchListener(this);
    Button1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            SoundManager.playSound(1, 1);
        }
    });
     }

 public void onDestroy()
    {
        super.onDestroy();
        SoundManager.cleanup();
    }

} }

Thank you so much 非常感谢

There is no big difference between click and touch. 点击和触摸之间没有太大区别。 But if you want to implement both, you have to make a trick to make it work. 但是,如果您想同时实现这两种方法,则必须要使它起作用。

For the System it is hard to decide laying the finger on a button, if you want to "click" or "touch" it. 对于系统,如果要“单击”或“触摸”它,则很难决定将手指放在按钮上。 To controll this, you can get the time, the button is clicked/touched/whatever... 为了控制这一点,您可以获得时间,单击/触摸/任何按钮...

for example: If you click/touch/whatever a Button you start counting, if you stopped clicking/touching/whateevring you stopped the countig. 例如:如果您单击/触摸/无论按钮如何,您都将开始计数;如果您停止单击/触摸/进行举报,则您将停止计数。 After that its easy: if(time < 1s) else 之后很容易:if(time <1s)else

Beware, that it would be a good idea to have a Click-Listener and TouchListener with the same code in it and just decide, using the counting stuff. 当心,最好在其中包含一个具有相同代码的Click-Listener和TouchListener,然后使用计数材料来决定。

I've implemeted it already at it works perfectly! 我已经实现了它,它可以完美运行!

例如,您可以使用GestureListener( https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html#5307216038506006379 )并处理按钮的不同事件或手势类型(触摸,点击,滑动) ...)

动态创建按钮会导致问题。但是如果在您的xml文件中创建按钮,然后设置为

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

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