我只是想创建我的应用程序,因此用户一次只能单击一个复选框。因此,当用户点击一个复选框时,其他2变为假。我尝试了一些东西,但似乎没有什么工作,我在网上找不到任何关于它的东西。谢谢- 这是我的代码......
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>
答案 0 :(得分:4)
有一个名为RadioButton的windget可以执行此功能。
单选按钮允许用户从一组中选择一个选项。如果您认为用户需要并排查看所有可用选项,则应使用单选按钮作为互斥的可选集。