我只是想让我的应用程序,使用户一次只能点击一个复选框。所以当用户点击一个复选框时,其他两个变成错误。我已经尝试了一些东西,但似乎没有任何工作,并且我在网上找不到任何东西。 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