如何使用切换按钮更改按钮的Onclick

时间:2017-08-14 05:42:34

标签: javascript android-studio

嗨我有一个切换按钮,当这个按钮被切换时,我会设置我的另一个下一个按钮点击这个

public void shuffleSong(View view)
    {
        //Call shuffle  in the Songcollection
        Song shuffle = SongCollection.shufflefunction(songId);

        songId = shuffle.getId();
        title = shuffle.getTitle();
        artist = shuffle.getArtist();
        fileLink = shuffle.getFileLink();
        coverArt = shuffle.getCoverArt();
        album=shuffle.getAlbum();

        url = BASE_URL + fileLink;

        displaySong(title, artist, coverArt);

        stopActivities();

        playOrPauseMusic(new View(this));


    }

当按钮没有切换时,我想将我的下一个按钮设置为on

 public void playNext(View view)
        {
            Song nextSong = SongCollection.getNextSong(songId);
            if (nextSong != null) {
                songId = nextSong.getId();
                title = nextSong.getTitle();
                artist = nextSong.getArtist();
                fileLink = nextSong.getFileLink();
                coverArt = nextSong.getCoverArt();
                album=nextSong.getAlbum();

                url = BASE_URL + fileLink;

                displaySong(title, artist, coverArt);

                stopActivities();

                playOrPauseMusic(view);
            }

        }

但是我不知道该怎么做

1 个答案:

答案 0 :(得分:0)

要检测用户何时激活按钮或开关,请创建一个CompoundButton.OnCheckedChangeListener对象,并通过调用setOnCheckedChangeListener()将其指定给该按钮。例如:

ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // The toggle is enabled, do whatever you want to do here
        } else {
            // The toggle is disabled, do whatever you want to do here
        }
    }
});