2014-11-05 92 views
0

我有3個切換按鈕。一次只允許一個打開。任何人都可以告訴我爲什麼其他兩個切換按鈕不會關閉,如果第三個按鈕被按下。當其他的Togglebutton被點擊時Togglebutton不會關閉

MusicPlayerActivity.java

package com.example.musicplayer; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ToggleButton; 

public class MusicPlayerActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.music_player); 
     addListenerOnButton(null); 
    } 

    public void addListenerOnButton(View view) { 
     ToggleButton artist = (ToggleButton) findViewById(R.id.artistID); 
     ToggleButton album = (ToggleButton) findViewById(R.id.albumID); 
     ToggleButton song = (ToggleButton) findViewById(R.id.songID); 

    } 
} 

music_player.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="${relativePackage}.${activityClass}" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="18dp" 
     android:background="#009ACD" 
     android:text="Play All Music" 
     android:textColor="#FFFFFF" /> 

<RadioGroup 
    android:id="@+id/toggleGroup" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <ToggleButton 
     android:id="@+id/albumID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="addListenerOnButton" 
     android:layout_alignBottom="@+id/artistID" 
     android:layout_toRightOf="@+id/artistID" 
     android:text="Album" 
     android:textOff="Album" 
     android:textOn="Album" /> 

    <ToggleButton 
     android:id="@+id/songID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/albumID" 
     android:onClick="addListenerOnButton" 
     android:layout_alignBottom="@+id/albumID" 
     android:layout_toRightOf="@+id/albumID" 
     android:text="Song" 
     android:textOff="Song" 
     android:textOn="Song" /> 

    <ToggleButton 
     android:id="@+id/artistID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="addListenerOnButton" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/button1" 
     android:layout_marginLeft="63dp" 
     android:layout_marginTop="16dp" 
     android:text="Artist" 
     android:textOff="Artist" 
     android:textOn="Artist" /> 
</RadioGroup> 
</RelativeLayout> 
+0

如果下面的答案之一幫助你,請選擇一個「接受的答案」。如果沒有,請讓我們知道您遇到的問題 – 2014-11-13 15:58:12

回答

1

問題是切換器isChecked按鈕的方法是不被解僱。

OPTION 1

在XML文件中,添加這一行的每個切換按鈕的

android:onClick="MethodName" 

<ToggleButton 
     android:id="@+id/albumID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/artistID" 
     android:layout_toRightOf="@+id/artistID" 
     android:text="Album" 
     android:textOff="Album" 
     android:textOn="Album" 
     android:onClick="onAlbumIDClicked" /> 

在你onCreate方法,創建相應的方法和然後檢查他們是否被檢查。這將工作。

OPTION 2

刪除所有的if語句,並添加以下

 artist.setOnCheckedChangeListener(changeChecker); 
     album.setOnCheckedChangeListener(changeChecker); 
     song.setOnCheckedChangeListener(changeChecker); 

     OnCheckedChangeListener changeChecker = new OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if (isChecked){ 
      if (buttonView == artist) { 
       artist.setChecked(true); 
       album.setChecked(false); 
       song.setChecked(false); 
      } 
      if (buttonView == album) { 
       artist.setChecked(false); 
       album.setChecked(true); 
       song.setChecked(false); 
      } 
      if (buttonView == song) { 
       artist.setChecked(false); 
       album.setChecked(false); 
       song.setChecked(true); 
      } 
     } 
    } 
}; 

OPTION 3這個代碼 - 在XML文件中使用無線電集團

,添加以下行的代碼包裝了3個切換按鈕。

<RadioGroup android:id="@+id/toggleGroup" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="24dp" 
       android:orientation="horizontal" 
      > 

在你的活動類,創建一個偵聽

static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(final RadioGroup radioGroup, final int i) { 
      for (int j = 0; j < radioGroup.getChildCount(); j++) { 
       final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j); 
       view.setChecked(view.getId() == i); 
      } 
     } 
    }; 

現在,在onCreate方法註冊監聽器。

((RadioGroup) findViewById(R.id.toggleGroup)).setOnCheckedChangeListener(ToggleListener); 

現在在onToggle方法中,根據id,進行相應設置。

public void onToggle(View view) { 
    ((RadioGroup)view.getParent()).check(view.getId()); 
    // app specific stuff .. 
} 
+0

@VinceEmigh,同意。有更好的方法來處理這個問題。但是我爲他在代碼中面臨的問題提供瞭解決方案。 – Prem 2014-11-05 20:57:33

+0

我更新了我的代碼。我添加了一個onclick =方法的名字,但它給了我一個nosuchmethodexception。爲什麼沒有看到我的方法「addListenerOnButton」? – 2014-11-05 21:07:18

+0

@VinceEmigh,在我的回答中添加了RadioGroup的詳細信息:) – Prem 2014-11-05 21:30:45

0

而應該使用一個RadioGroup項目已被選定後,管理去選擇任何其他選定的項目。它被稱爲「多重排除」,它確保一次只選擇一個項目。

<RadioGroup 
    android:id="@+id/toggleGroup" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <ToggleButton 
     android:id="@+id/albumID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="addListenerOnButton" 
     android:layout_alignBottom="@+id/artistID" 
     android:layout_toRightOf="@+id/artistID" 
     android:text="Album" 
     android:textOff="Album" 
     android:textOn="Album" /> 

    <ToggleButton 
     android:id="@+id/songID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/albumID" 
     android:onClick="addListenerOnButton" 
     android:layout_alignBottom="@+id/albumID" 
     android:layout_toRightOf="@+id/albumID" 
     android:text="Song" 
     android:textOff="Song" 
     android:textOn="Song" /> 

    <ToggleButton 
     android:id="@+id/artistID" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="addListenerOnButton" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/button1" 
     android:layout_marginLeft="63dp" 
     android:layout_marginTop="16dp" 
     android:text="Artist" 
     android:textOff="Artist" 
     android:textOn="Artist" /> 
</RadioGroup> 

此組將確保一次只選擇1個ToggleButton。 現在,如果您想爲此添加偵聽器,則必須指定onClick屬性。對於該值,您可以指定要執行的方法。

的方法必須

  • 是公共
  • 返回void
  • 定義視圖作爲其唯一的參數(這將是爲點擊查看)

這個問題你addListenerOnButton方法是它不聲明View參數。你的方法應該看起來像

public void addListenerOnButton(View view) { 

} 

而且,由於你有RadioGroup,你不再需要手動取消選擇其他按鈕。

+0

我刪除了手動取消選擇並更新了我的代碼。但現在的問題是,如果我點擊一個按鈕,然後點擊另一個按鈕,它們都會亮起。我必須再次點擊按鈕來關閉按鈕,這是我不想要的? – 2014-11-05 22:50:17

+0

@gmanplex Android的'RadioGroup'與ButtonGroup非常相似(如果你曾經使用它的話)。如果兩個按鈕位於同一組中,則不能同時選擇它們。現在,您正在手動處理取消選擇,因爲您沒有系統可以爲您進行管理。這就是'RadioGroup'的用途。它確保一次只能選擇組中的1個按鈕 – 2014-11-05 22:53:06