能够与海誓山盟多个onTouch方法呢? [英] Ability to have multiple onTouch methods with eachother?

查看:216
本文介绍了能够与海誓山盟多个onTouch方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

startButton 有一个onTouchListener,当pressed,启动媒体播放器( MP 在我源),使相同的按钮有一个停止功能吧。

The startButton has an onTouchListener that, when pressed, starts a media player (mp in my source) and makes the same button have a stop function to it.

例如:
用户点击启动按钮和播放音乐。然后用户点击相同的按钮(现在已经停止的的setText)停止音乐播放器。

For example: The user hits the start button and the music plays. Then the user hits the same button (which now has a setText of "Stop") to stop the music player.

是否有可能有一个方法中的多个onTouchListener(S)?

Is it possible to has multiple onTouchListener(s) within a method?

startButton.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                mp.start();
                timeLeft.setText("Status: Initiated");
                startButton.setText("Stop Dreaming");

                startButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        mp.stop();

                    }

                });

这是我目前有...我将如何正确设置?

This is what I currently have... How would I set this up correctly?

推荐答案

我建议你使用一个切换按钮,用于这项工作:

I suggest you use a toggle button for this endeavor:

public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
        mp.start();
            timeLeft.setText("Status: Initiated");
            startButton.setText("Stop Dreaming");
    } else {
        mp.stop();
    }
}

在这里切换按钮更多信息: http://developer.android .COM /引导/主题/ UI /控制/ togglebutton.html

More info on toggle buttons here: http://developer.android.com/guide/topics/ui/controls/togglebutton.html

这篇关于能够与海誓山盟多个onTouch方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆