如何根据ToggleButton状态使我的ImageButton表现不同?

时间:2017-05-12 19:16:31

标签: android boolean android-imagebutton android-togglebutton

我正在尝试添加切换按钮,以便我的图像按钮根据切换按钮的状态切换其翻译的方向。但是,在尝试将其添加到媒体播放器时以及使用if语句时,我遇到了问题。

public class MainActivity extends AppCompatActivity {
    TextToSpeech tts1;
    ToggleButton toggleButton;

    public void changeTranslation(View view){
        boolean on = ((ToggleButton)view).isChecked();
        if(on){
            toggleButton.setText("Speech-to-Farts");
            Toast.makeText(getApplicationContext(),"Translating speech to farts.",Toast.LENGTH_SHORT);
        } else{
            toggleButton.setText("Farts-to-Speech");
            Toast.makeText(getApplicationContext(),"Translating farts to speech.",Toast.LENGTH_SHORT);

        }
    }

    private static final String TAG = "MainActivity";
    private MediaPlayer mMediaPlayer;
    private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener(){
        @Override
        public void onCompletion(MediaPlayer mediaPlayer){
            releaseMediaPlayer();
        }
    };

    private void releaseMediaPlayer(){
        if(mMediaPlayer != null){
            mMediaPlayer.release();
            mMediaPlayer=null;
        }
    }

    TextView tv1;
    ImageButton b1;

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

        tv1 = (TextView) findViewById(textView);
        b1 = (ImageButton) findViewById(R.id.recordButton);

        final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle_button);
        toggleButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (toggleButton.isChecked()) {
                    toggleButton.setText("Farts-to-Speech");
                } else {
                    toggleButton.setText("Speech-to-Farts");

                }
            }
        });

        tts1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    tts1.setLanguage(Locale.US);
                }
            }
        });

        b1.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                int[] fartLibrary={};
                String[] speechLibrary={};

                if(toggleButton==true) {
                    releaseMediaPlayer();
                    String randomTranslation = (speechLibrary[new Random().nextInt(speechLibrary.length)]);
                    tts1.speak(randomTranslation, TextToSpeech.QUEUE_FLUSH, null);
                } else {
                    releaseMediaPlayer();
                    mMediaPlayer=MediaPlayer.create(this,fartLibrary);
                    mMediaPlayer.start();
                    mMediaPlayer.setOnCompletionListener(mCompletionListener);
                }
            }
        });
    }
}

这是我的Translations.java。我不确定这是否有必要,但

public class Translations{

private String mSpeechTranslation;

private int mFartTranslation;

public Translations(String speechTranslation, int fartTranslation){
    mFartTranslation = fartTranslation;
    mSpeechTranslation = speechTranslation;
}

public String getmSpeechTranslation(){ return mSpeechTranslation;}

public int getmFartTranslation(){ return mFartTranslation;}
}

0 个答案:

没有答案