Change button text and action - android development

前端 未结 2 1842
攒了一身酷
攒了一身酷 2020-12-01 04:32

I\'m having trouble figuring out how to change the text and action of a button. What I want to do is have a button with the text \"play\" and when clicked it will play a son

相关标签:
2条回答
  • 2020-12-01 05:26
    private bool isPlaying=false;
    final Button testButton = (Button) findViewById(R.id.button1);
    testButton.setText("Play");
    testButton.setOnClickListener( new View.OnClickListener() {
    
    @Override
    public void onClick (View v) {
    if(!isPlaying){
      mPlayer.start();
      testButton.setText("Pause");
      isPlaying=true;
    }else{
      mPlayer.stop();
      testButton.setText("Play");
      isPlaying=false;
    }
    

    I thing you've got the idea. Though, I'm not sure about MediaPlayer states.

    0 讨论(0)
  • 2020-12-01 05:30

    You can use setTag. So, your code will look like,

    final Button testButton = (Button) findViewById(R.id.button1);
    testButton.setTag(1);
    testButton.setText("Play");
    testButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            final int status =(Integer) v.getTag();
            if(status == 1) {
                mPlayer.start();
                testButton.setText("Pause");
                v.setTag(0); //pause
            } else {
                testButton.setText("Play");
                v.setTag(1); //pause
            }
        }
    });
    

    About setTag

    0 讨论(0)
提交回复
热议问题