2017-04-15 2 views
3

Ich habe den einfachen Code unten und ich kann nicht verstehen, warum es für mich nicht arbeiten würde. Was ich einfach tun möchte, ist "männlich" oder "weiblich" anzuzeigen (toast), je nachdem, welchen Radio Button ich angeklickt habe. HierKann keinen Wert der Taste in der Radiogruppe anzeigen

ist der Code aus der Haupttätigkeit:

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(); 
       } 
      } 
    ); 
} 

Und die Radiogruppe:

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

Kann jemand bitte darauf hinweisen, was ich fehle? Das ist frustrierend. this

+0

alles aus dem Code in Ordnung scheint, Sie haben geschrieben, Sie können etwas mehr für Kontext hinzufügen müssen, ist die onClickListenerButton() wird aufgerufen? –

+0

@BradleyWilson, nein, das ist so ziemlich alles, was ich habe. Ich rufe es nicht an und ich bin mir nicht sicher, ob ich weiß wie. –

+1

Okay, keine Sorgen. Ich werde dir jetzt ein kleines Beispiel schreiben. –

Antwort

1

Ihr Code muss wie folgt aussehen:

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(); 
    } 

dieses Probieren Sie es funktioniert gut ich es versuchen.

1

prüfen es ist auch ähnlich ..

in Ihrem onCreate() Anruf

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

    onClickListenerButton(); //add this 

    } 
1

Dies ist ein Beispiel dafür, was die vollständige Klasse aussehen soll, müssen Sie einige der Forschung in die tun Activity life-cycle zu verstehen, welche Methoden zu welcher bestimmten Zeit aufgerufen werden. Sie werden dann das Konzept einer Aktivität mehr verstehen und wie es präsentiert werden soll.

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(); 
       } 
      } 
    ); 

} 

} 

Hinweis: ich dies aus der Spitze von meinem Kopf geschrieben, ich habe es nicht getestet.

0

Hier ist man Code in Aktivität Struktur

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(); 
        } 
       } 
     ); 
    } 

Und XML Layout

<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> 
Verwandte Themen