简体   繁体   中英

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. if Button1 is clicked, then the sound is sound1 but if Button1 is touched by the gesture, the sound is sound2. to handle the sound, I use a SoundManager class. 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

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.

I've implemeted it already at it works perfectly!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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