【问题标题】:How to call information from radiobutton in separate activity in Android Studio如何在 Android Studio 的单独活动中从单选按钮调用信息
【发布时间】:2021-03-14 22:23:33
【问题描述】:

在我的应用程序中,我在应用程序启动时播放音乐。如果用户进入设置,他们可以更改音量或打开和关闭音乐,但是我不知道如何从单选按钮调用信息以在其他活动中打开或关闭音乐。我知道如果我在 onCreate 下的主要活动中关闭音乐,它将收听单选按钮,但我希​​望在应用启动时播放音乐,当您恢复活动时,它会从单选按钮输入信息。

这是我主要活动的代码


import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;

import android.os.Bundle;

import android.view.View;




public class MainActivity extends AppCompatActivity {




        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Intent svc = new Intent(this, music.class
            );
            startService(svc);

        }

        public void onPlayPressed(View view) {

            Intent myIntent = new Intent(getBaseContext(), Game.class);
            startActivity(myIntent);

        }

        public void onSettingsPressed(View view) {

            Intent myIntent = new Intent(getBaseContext(), Settings.class);
            startActivity(myIntent);

        }

        public void onHowtoPressed(View view) {

            Intent myIntent = new Intent(getBaseContext(), HowtoPlay.class);
            startActivity(myIntent);

        }

        @Override
        public void onResume() {

            super.onResume();

            
        }



        @Override
        public void onDestroy() {


            super.onDestroy();
            Intent svc = new Intent(this, music.class);
            stopService(svc);


        }
    }

这是我的设置活动的代码


import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;

public class Settings extends AppCompatActivity {


    private RadioGroup radioGroup;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
            final Intent svc = new Intent(this, music.class
            );


            RadioButton rdb = (RadioButton) findViewById(R.id.on);
            rdb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean checked = ((RadioButton) v).isChecked();
                    // Check which radiobutton was pressed
                    if (checked){
                        startService(svc);

                    }
                    else{
                        stopService(svc);
                    }
                }
            });
            RadioButton rdb2 = (RadioButton) findViewById(R.id.off);
            rdb2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean checked = ((RadioButton) v).isChecked();
                    // Check which radiobutton was pressed
                    if (checked){
                        stopService(svc);

                    }
                    else{
                        startService(svc);
                    }
                }
            });
            final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        SeekBar volControl = (SeekBar)findViewById(R.id.seekBar);
        volControl.setMax(maxVolume);
        volControl.setProgress(curVolume);
        volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
            }
        });



    }

        public void onMainMenuPressed(View view){

        Intent myIntent=new Intent(getBaseContext(),MainActivity.class);
        startActivity(myIntent);
    }

        public void onResumePressed (View view){

        Intent myIntent=new Intent(getBaseContext(),Game.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(myIntent);
        finish();
    }
    } ```

【问题讨论】:

    标签: java android radio-button


    【解决方案1】:

    使用带有简单布尔标志的 SharedPreferences。

    您应该在单选组上使用 onCheckedChangeListener 而不是在单个单选按钮上使用 onClickListener,如下所示:

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(...) {
            getSharedPreferences("prefs", 0).edit().putBoolean("music", rdb.isChecked()).apply();
        }
    });
    

    然后在其他活动中:

    boolean music = getSharedPreferences("prefs", 0).getBoolean("music", true);
    

    然后检查flag的值来决定是否启动音乐服务。

    【讨论】:

    • 感谢您的回答!但我无法让它发挥作用。首先,当我粘贴 OnCheckedChangeListener 时,Android Studio 想要我实现方法,省略号应该放在哪里?
    • 唯一需要实现的方法是onCheckedChanged()。您需要两个椭圆所在的参数,一个是RadioGroup 类型,一个是int 类型,如here 所述。我忽略了包含该信息,因为如果您开始在 setOnCheckedChangeListener() 方法的括号内输入 new RadioGroup.OnCheckedChangeListener(),Android Studio 可以为您自动完成方法实现。
    • 我明白了,谢谢!那么如何检查标志的值呢?
    • 就像您在 onClick() 方法中所做的一样:if (music) { startService(svc); }。您显然还需要在其他活动中定义 svc 变量。
    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多