2015-11-08 45 views
0

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

然后,您可能需要将复选框更改为单选按钮。 http://developer.android.com/guide/topics/ui/controls/radiobutton.html。那么你不必担心其他的检查框。 – rpirsc13

+0

[只选择一个复选框]可能的重复(http://stackoverflow.com/questions/4844190/select-only-one-checkbox) – darch

回答

4

有一个名为RadioButton它,这种功能windget。

单选按钮允许用户从一组中选择一个选项。如果您认为用户需要并排查看所有可用选项,则应该使用单选按钮作为互斥的可选集。

+0

您可以将单选按钮放入首选项屏幕吗? –

+0

@LeytonTaylor看看这个[教程](https://androidresearch.wordpress.com/2012/03/09/creating-a-preference-activity-in-android/) –

+0

我应该改进我的答案吗?给你? –

相关问题