按钮onClickListener无法在Android中播放声音

学向勤中得,萤窗万卷书。这篇文章主要讲述按钮onClickListener无法在Android中播放声音相关的知识,希望能为你提供帮助。
我试图设置我的按钮,点击时播放一首歌,但没有任何反应,没有错误,没有强制退出。当我单击渲染中的按钮时,它只是按下。

public class Shows extends AppCompatActivity { RadioButton radRent1, radRent2, radWicked, radHamilton, radPhantom; Button btnRent, btnWicked, btnHamilton, btnPhantom; MediaPlayer mpRent1,mpRent2, mpWicked, mpHamilton, mpPhantom; int playing; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shows); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(Shows.this, ItemListActivity.class)); } }); playing = 0; mpRent1 = new MediaPlayer(); mpRent1 = MediaPlayer.create(this, R.raw.seasons_of_love); mpRent2 = new MediaPlayer(); mpRent2 = MediaPlayer.create(this, R.raw.seasons_of_love); btnRent = (Button)findViewById(R.id.btnRent); btnRent.setOnClickListener(bRent); } Button.OnClickListener bRent = new Button.OnClickListener() { @Override public void onClick(View v) { switch(playing){ case 0: mpRent1.start(); playing = 1; btnRent.setText("Pause Selected Song"); break; case 1: mpRent1.pause(); playing = 0; btnRent.setText("Play Selected Song"); break; } } }; }

任何人都可以看到错误在哪里?
答案您需要打开View的ID
或者在bRent上使用set onClickListener
`
bRent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view){ //put your logic here }`

另一答案以这种方式设定行动
playbutton.setOnClickListener(this); pausebutton.setOnClickListener(this);

然后实现这一点
implements View.OnClickListener

【按钮onClickListener无法在Android中播放声音】而不是覆盖
@Override public void onClick(View view) { switch(view.getId()) { case R.id.play: //Play voicefilebreak; case R.id.pause: //Stop MediaPlayerbreak; } }


    推荐阅读