我有两个按钮(到达和离开)我想在两个按钮的点击事件上播放声音,并在它们之间切换 requestfocus.. 这是一个问题
这是我第一次单击“到达按钮”或“出发”按钮,它的工作正常(播放声音和焦点),但是当我单击时,例如单击“出发”按钮后到达,它没有播放声音,也没有显示吐司。
这是我的代码示例
Button btnArrival, btnDeparture, btnScan;
View cameraView;
MediaPlayer mPlayer;
boolean in, out;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnArrival = (Button) findViewById(R.id.btnArrival);
btnDeparture = (Button) findViewById(R.id.btnDeparture);
btnScan = (Button) findViewById(R.id.btnScan);
cameraView = findViewById(R.id.cameraView);
cameraView.setVisibility(View.GONE);
//cameraLayout.setEnabled(false);
btnScan.setEnabled(false);
}
public void onArrivalClicked(View v){
MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.clickbeep);
mPlayer.start();
btnScan.setEnabled(true);
in = true;
out = false;
Toast.makeText(MainActivity.this, "Arrival", Toast.LENGTH_SHORT).show();
focusArrivalBtn();
}
private void focusArrivalBtn() {
btnArrival.setFocusableInTouchMode(true);
btnArrival.requestFocus();
}
public void onDepartureClicked(View v){
MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.clickbeep);
mPlayer.start();
btnScan.setEnabled(true);
in = false;
out = true;
Toast.makeText(MainActivity.this, "Departure", Toast.LENGTH_SHORT).show();
focusDepartureBtn();
}
private void focusDepartureBtn() {
btnDeparture.setFocusableInTouchMode(true);
btnDeparture.requestFocus();
}