2015-11-08 46 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

我應該改進我的答案嗎?給你? –

相關問題