Studio中制作随机播放列表按钮和重复按钮

Studio中制作随机播放列表按钮和重复按钮

本文介绍了如何在Android Studio中制作随机播放列表按钮和重复按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将此代码转换为android studio,我在上面停留了2天,无法弄清楚Plz可以帮助我

I dont know how to convert this code to android studio i am stuck on it for 2 days and cant figure it out Plz help me

btnRepeat.setOnClickListener(new View.OnClickListener(){

btnRepeat.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(isRepeat){
                isRepeat = false;
                Toast.makeText(getApplicationContext(), "Repeat is OFF", Toast.LENGTH_SHORT).show();
                btnRepeat.setImageResource(R.drawable.btn_repeat);
            }else{
                // make repeat to true
                isRepeat = true;
                Toast.makeText(getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();
                // make shuffle to false
                isShuffle = false;
                btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
                btnShuffle.setImageResource(R.drawable.btn_shuffle);
            }
        }
    });

和另一个

btnShuffle.setOnClickListener(new View.OnClickListener(){

btnShuffle.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(isShuffle){
                isShuffle = false;
                Toast.makeText(getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
                btnShuffle.setImageResource(R.drawable.btn_shuffle);
            }else{
                // make repeat to true
                isShuffle= true;
                Toast.makeText(getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
                // make shuffle to false
                isRepeat = false;
                btnShuffle.setImageResource(R.drawable.btn_shuffle_focused);
                btnRepeat.setImageResource(R.drawable.btn_repeat);
            }
        }
    });

这是最后的代码

@Override公共无效onCompletion(MediaPlayer arg0){

@Override public void onCompletion(MediaPlayer arg0) {

    // check for repeat is ON or OFF
    if(isRepeat){
        // repeat is on play same song again
        playSong(currentSongIndex);
    } else if(isShuffle){
        // shuffle is on - play a random song
        Random rand = new Random();
        currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
        playSong(currentSongIndex);
    } else{
        // no repeat or shuffle ON - play next song
        if(currentSongIndex < (songsList.size() - 1)){
            playSong(currentSongIndex + 1);
            currentSongIndex = currentSongIndex + 1;
        }else{
            // play first song
            playSong(0);
            currentSongIndex = 0;
        }
    }
}

推荐答案

您应该使用切换按钮之类的东西!而不是普通的按钮,因此您不必担心打开和关闭了什么!这里是JavaDoc:

You should use something like a toggle button! Instead of a normal button so you don't have to care what is switched on and off! Here the JavaDoc:

http://developer.android.com/reference/android/widget/ToggleButton.html

此链接包含一个很好的教程,介绍如何在您的代码中使用它:

And this link contains a nice tutorial on how to use it within your code:

http://www.mkyong.com/android/android-togglebutton-example/

这篇关于如何在Android Studio中制作随机播放列表按钮和重复按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:29