2017-04-15 30 views
3

我有下面的簡單代碼,我不明白爲什麼它不適用於我。 我只是想根據我點擊哪個單選按鈕來顯示(烤麪包)「男性」或「女性」。無法顯示廣播組中按鈕的值

以下是主要活動代碼:

private static RadioGroup radio_g; 
private static RadioButton radio_b; 
private static Button button_sbm; 


public void onClickListenerButton() { 
    radio_g = (RadioGroup)findViewById(R.id.genderButton); 
    button_sbm = (Button)findViewById(R.id.getBMI); 

    button_sbm.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        int selected_id = radio_g.getCheckedRadioButtonId(); 
        radio_b = (RadioButton)findViewById(selected_id); 
        Toast.makeText(MainActivity.this, 
          radio_b.getText().toString(),Toast.LENGTH_SHORT).show(); 
       } 
      } 
    ); 
} 

和無線電組:

<RadioGroup 
    android:id="@+id/genderButton" 
    android:layout_width="124dp" 
    android:layout_height="67dp" 
    android:layout_marginLeft="89dp" 
    android:layout_marginStart="88dp" 
    android:layout_marginTop="63dp" 
    android:visibility="visible" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/ageField" 
    tools:layout_constraintLeft_creator="1" 
    tools:layout_constraintTop_creator="1"> 

    <RadioButton 
     android:id="@+id/maleButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:checked="false" 
     android:text="Male" 
     tools:text="male" /> 

    <RadioButton 
     android:id="@+id/femaleButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Female" 
     tools:text="female" /> 
</RadioGroup> 

有人可以,請指出我錯過了什麼?這令人沮喪。

+0

一切似乎從您發佈的代碼沒關係,你可能需要添加一些更多的情況下,是onClickListenerButton()被調用? –

+0

@BradleyWilson,不,這幾乎是我所有的代碼。我不叫它,我不知道我知道如何。 –

+1

好的,不用擔心。我現在寫一個小例子。 –

回答

1

你的代碼必須是這樣的:

public class FormActivity extends Activity implements View.OnClickListener { 

private static RadioGroup radio_g; 
private static RadioButton radio_b; 
private static Button button_sbm; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_form); 
     // initialize component here like this 
    radio_g = (RadioGroup)findViewById(R.id.genderButton); 
    button_sbm = (Button)findViewById(R.id.getBMI); 

     // Then call listener on button click like this 
     button_sbm .setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
    int selected_id = radio_g.getCheckedRadioButtonId(); 
        radio_b = (RadioButton)findViewById(selected_id); 
        Toast.makeText(MainActivity.this, 
          radio_b.getText().toString(),Toast.LENGTH_SHORT).show(); 
    } 

試試這個它工作正常我嘗試它。

1

檢查this它也是類似的。在您的通話onCreate()

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    onClickListenerButton(); //add this 

    } 
1

這是滿級應該是什麼樣子的例子,你需要做一些研究成Activity life-cycle瞭解在特定時間調用哪些方法。然後,您將更多地瞭解活動的概念以及應該如何呈現活動。

public class MainActivity extends Activity { 

// global instances 
private static RadioGroup radio_g; 
private static RadioButton radio_b; 
private static Button button_sbm; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    onClickListenerButton(); 
} 

public void onClickListenerButton() { 
    radio_g = (RadioGroup)findViewById(R.id.genderButton); 
    button_sbm = (Button)findViewById(R.id.getBMI); 

    button_sbm.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        int selected_id = radio_g.getCheckedRadioButtonId(); 
        radio_b = (RadioButton)findViewById(selected_id); 
        Toast.makeText(MainActivity.this, 
          radio_b.getText().toString(),Toast.LENGTH_SHORT).show(); 
       } 
      } 
    ); 

} 

} 

注意:我寫這從我的頭頂,我沒有測試它。

0

這裏是你在活動結構代碼

public class radioToButton extends AppCompatActivity { 

    private static RadioGroup radio_g; 
    private static RadioButton radio_b; 
    private static Button button_sbm; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_radio_to_button); 

     radio_g = (RadioGroup) findViewById(R.id.genderButton); 
     button_sbm = (Button) findViewById(R.id.getBMI); 

     button_sbm.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int selected_id = radio_g.getCheckedRadioButtonId(); 
         radio_b = (RadioButton) findViewById(selected_id); 
         Toast.makeText(radioToButton.this, 
           radio_b.getText().toString(), Toast.LENGTH_SHORT).show(); 
        } 
       } 
     ); 
    } 

和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"> 

    <RadioGroup 
     android:id="@+id/genderButton" 
     android:layout_width="124dp" 
     android:layout_height="67dp" 
     android:layout_marginLeft="89dp" 
     android:layout_marginStart="88dp" 
     android:layout_marginTop="63dp" 
     android:visibility="visible" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintTop_creator="1"> 

     <RadioButton 
      android:id="@+id/maleButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:checked="false" 
      android:text="Male" 
      tools:text="male" /> 

     <RadioButton 
      android:id="@+id/femaleButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Female" 
      tools:text="female" /> 
    </RadioGroup> 

    <Button 
     android:layout_below="@id/genderButton" 
     android:text="AAAAAAAA" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/getBMI"/> 
</RelativeLayout>