有人知道如何在Android中爲按鈕添加單擊聲音嗎?這裏是我的代碼如下。 我檢查了一些教程,但我不知道如何實現它們在我的代碼上。Android單擊聲音
http://s822.photobucket.com/user/backalleylive/media/android_test_error.jpg.html
public class MainActivity extends Activity implements OnClickListener {
MediaPlayer clicksound;
public void onClick(View a){
switch (a.getId()){
case R.id.login:
Intent b = new Intent (this, play.class);
startActivity(b);
break;
case R.id.admin:
Intent c = new Intent (this, admin.class);
startActivity(c);
break;
case R.id.exit:
finish();
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View playButton = findViewById(R.id.login);
playButton.setOnClickListener(this);
View adminButton = findViewById(R.id.admin);
adminButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit);
exitButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
通常情況下,這是沒有必要的。通過向視圖添加點擊偵聽器,只要用戶沒有禁用它們,默認點擊就會播放。如果用戶已經禁用了點擊聲音,那麼不要因爲彈奏而變成混蛋。 – Krylez
可能的重複[如何使用默認點擊聲音使用view.playSoundEffect(SoundEffectConstants.CLICK);](http://stackoverflow.com/questions/13680163/how-do-i-use-the-default-click -sound-using-view-playsoundeffectsoundeffectconconst) – Krylez