我只是想讓我的應用程序,使用戶一次只能點擊一個複選框。所以當用戶點擊一個複選框時,其他兩個變成錯誤。我已經嘗試了一些東西,但似乎沒有任何工作,並且我在網上找不到任何東西。 Thanks- 這裏是我的代碼...我有多個複選框,但只希望其中一個點擊一次
public void buttonClick() {
imgView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
blade = (ImageView)findViewById(R.id.imageView4);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
standardSound = MediaPlayer.create(this, R.raw.file.mp3);
alternateSound = MediaPlayer.create(this, R.raw.file.mp3);
whiteSound = MediaPlayer.create(this, R.raw.file.mp3);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean alternate = getPrefs.getBoolean("alternate", false);
boolean white = getPrefs.getBoolean("white", false);
boolean standard = getPrefs.getBoolean("standard",false);
if (blade.getAnimation() == null) {
// no animation, start it
if (alternate == true){
alternateSound.start();
blade.startAnimation(animRotate);
} else if (white == true){
whiteSound.start();
blade.startAnimation(animRotate);
} else if (standard == true) {
standardSound.start();
blade.startAnimation(animRotate);
}
} else {
//animation is showing, stop it
blade.clearAnimation();
standardSound.stop();
standardSound.prepareAsync();
whiteound.stop();
whiteSound.prepareAsync();
alternateSound.stop();
alternateSound.prepareAsync();
}
current_image_index++;
current_image_index = current_image_index % images.length;
imgView.setImageResource(images[current_image_index]);
imgView.invalidate();
}
}
);
}
而且我的XML
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Sound Settings">
<CheckBoxPreference
android:key="standard"
android:title="Standard Fan"
/>
<CheckBoxPreference
android:key="alternate"
android:title="Alternate Fan"
/>
<CheckBoxPreference
android:key="white"
android:title="White Noise"
/>
</PreferenceCategory>
</PreferenceScreen>
然後,您可能需要將複選框更改爲單選按鈕。 http://developer.android.com/guide/topics/ui/controls/radiobutton.html。那麼你不必擔心其他的檢查框。 – rpirsc13
[只選擇一個複選框]可能的重複(http://stackoverflow.com/questions/4844190/select-only-one-checkbox) – darch