2017-05-18 92 views
0

我想從檢查RadioButton獲取值。 RadioGroup中有三個RadioButton(具有不同的值)。那麼,我第一次檢查一個RadioButton,我能夠得到這個值,但是如果我改變到另一個RadioButton,我就無法再獲取值了。這就像是,如果onCheckedChanged只是第一次檢查RadioButton時調用。onCheckChanged僅被稱爲第一次

任何有用的將不勝感激。 在此先感謝。

這是我的代碼:

public class AsesorNutri extends AppCompatActivity { 


RadioGroup deporte; 
RadioButton andar; 
RadioButton correr; 
RadioButton ciclismo; 
String TAG=""; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_asesor_nutri); 
    andar=(RadioButton)findViewById(R.id.andar); 
    andar.setChecked(true); 
    correr=(RadioButton)findViewById(R.id.correr); 
    ciclismo=(RadioButton)findViewById(R.id.ciclismo); 
    deporte = (RadioGroup)findViewById(R.id.deporte); 


    deporte.setOnCheckedChangeListener(cambiodeporte); 

} 

    public RadioGroup.OnCheckedChangeListener cambiodeporte=new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { 
      switch(i) 
      { 
       case R.id.andar: 
        Log.i(TAG, "cambia andar"); 

        // do what you wan there 
        break; 
       case R.id.correr: 
        Log.i(TAG, "cambia correr"); 

        break; 

       case R.id.ciclismo: 
        Log.i(TAG, "cambia correr"); 


        break; 
      } 
     } 
    }; 



} 

這是我的佈局:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/MainActivity" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clickable="true" 

tools:context="lnspad.fitnessup.AsesorNutri"> 


<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginStart="24dp" 
    android:layout_marginTop="67dp" 
    android:text="Seleccione Deporte" 
    android:textSize="18sp" /> 

<RadioGroup 
    android:id="@+id/deporte" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginStart="36dp" 
    android:layout_marginTop="18dp" 
    android:layout_below="@+id/textView3" 
    android:layout_alignStart="@+id/textView3"> 

    <RadioButton 
     android:id="@+id/andar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignStart="@+id/textView3" 
     android:layout_alignTop="@+id/deporte" 
     android:text="Andar" /> 



<RadioButton 
    android:id="@+id/correr" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Correr" 
    android:layout_marginEnd="39dp" 
    android:layout_alignBaseline="@+id/ciclismo" 
    android:layout_alignBottom="@+id/ciclismo" 
    android:layout_toStartOf="@+id/ciclismo" /> 

<RadioButton 
    android:id="@+id/ciclismo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Ciclismo" 
    android:layout_below="@+id/deporte" 
    android:layout_alignParentEnd="true" 
    android:layout_marginEnd="49dp" /> 


    </RadioGroup> 

    </RelativeLayout> 
+0

請輸入您的佈局。 – CoolMind

+0

@CoolMind佈局貼子 – ilpadrino

回答

0

您的代碼爲奇數。不知道你爲什麼在偵聽器上有公共訪問修飾符。爲什麼不用與其他變量相同的方式來定義呢?註釋聲稱它是一個資源ID,並且它是不變的,所以它必須工作。否則,它可能是一個生命週期問題,因爲你在onCreate()中進行初始化。

EG:

RadioGroup deporte; 
RadioButton andar; 
RadioButton correr; 
RadioButton ciclismo; 
RadioGroup.OnCheckedChangeListener cambiodeporte; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_asesor_nutri); 
    andar=(RadioButton)findViewById(R.id.andar); 
    andar.setChecked(true); 
    correr=(RadioButton)findViewById(R.id.correr); 
    ciclismo=(RadioButton)findViewById(R.id.ciclismo); 
    deporte = (RadioGroup)findViewById(R.id.deporte); 
    cambiodeporte = new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { 
     switch(i) 
     { 
      case R.id.andar: 
       Log.i(TAG, "cambia andar"); 

       // do what you wan there 
       break; 
      case R.id.correr: 
       Log.i(TAG, "cambia correr"); 

       break; 

      case R.id.ciclismo: 
       Log.i(TAG, "cambia correr"); 


       break; 
      } 
     } 
    }; 

    deporte.setOnCheckedChangeListener(cambiodeporte); 
} 
+0

@一個人它不起作用。同樣的行爲。我不知道這個活動是否從MainActivity開始與這個問題有任何關係。 – ilpadrino

0

你的代碼工作。我改變了一下,並表示敬酒。

private RadioGroup.OnCheckedChangeListener cambiodeporte=new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { 
     switch (i) { 
      case R.id.andar: 
       Log.i(TAG, "cambia andar"); 
       Toast.makeText(MainActivity.this, "1. cambia andar", Toast.LENGTH_SHORT).show(); 

       break; 
      case R.id.correr: 
       Log.i(TAG, "cambia correr"); 
       Toast.makeText(MainActivity.this, "2. cambia correr", Toast.LENGTH_SHORT).show(); 

       break; 

      case R.id.ciclismo: 
       Log.i(TAG, "cambia ciclismo"); 
       Toast.makeText(MainActivity.this, "3. cambia ciclismo", Toast.LENGTH_SHORT).show(); 

       break; 
     } 
    } 
}; 

也許這是一個設備問題。

+0

是的......它的工作原理。我認爲問題在於日誌不起作用。這就是爲什麼我信服它沒有工作。吐司顯示代碼有效。感謝您的回覆。 – ilpadrino

+0

@ilpadrino,我也注意到你的日誌包含了錯誤的值(兩種情況下的「cambia correr」)。所以,我改變了日誌並添加了敬酒。 – CoolMind

相關問題