2017-01-26 5 views
0

Ich mache eine App, wo es zwei Arten von Benutzern braucht. "Zuschauer" und "Auftragnehmer". Ich habe zwei Optionsfelder für jede Option erstellt. Ich möchte drei Dinge wissen: So aktivieren Sie eine Schaltfläche, wenn ein Optionsfeld ausgewählt ist. So deaktivieren Sie eine Schaltfläche, wenn keine Optionsschaltfläche ausgewählt ist. Zuletzt, wie Sie beide Radio-Buttons senden Sie eine einzigartige Aktivität abhängig von der gewählten Option. Zum Beispiel, ich wähle "Contractor" und dann die Taste, um fortzufahren, es wird mich zu einem einzigartigen Layout senden, das mit diesem Radio-Button verbindet.Wie man eine Radio-Taste eine Taste steuern

Hier ist meine XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/b" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/gradient_background" 
tools:context="com.devteam.abire.abire.b"> 

<android.support.v7.widget.CardView 
    app:cardElevation="15dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:layout_width="300dp" 
    android:layout_height="345dp"> 

</android.support.v7.widget.CardView> 

<android.support.v7.widget.CardView 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    app:cardElevation="20dp" 
    android:layout_width="320dp" 
    android:layout_height="320dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <View 
      android:background="#141526" 
      android:layout_width="match_parent" 
      android:layout_height="50dp"/> 

     <ImageView 
      android:id="@+id/abire_app_icon_v2" 
      android:layout_marginTop="21dp" 
      android:elevation="45dp" 
      android:layout_centerHorizontal="true" 
      android:background="@drawable/abire_logo_v1" 
      android:layout_width="55dp" 
      android:layout_height="55dp" /> 

     <TextView 
      android:layout_marginStart="20dp" 
      android:id="@+id/register_as_text" 
      android:layout_marginTop="10dp" 
      android:text="Register As A..." 
      android:textColor="#141526" 
      android:layout_below="@+id/abire_app_icon_v2" 
      android:textSize="28sp" 
      android:textAlignment="textStart" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <RadioButton 
      android:layout_marginStart="20dp" 
      android:textSize="22sp" 
      android:textColor="#141526" 
      android:id="@+id/viewer_radioBtn" 
      android:text="Viewer" 
      android:layout_marginTop="18dp" 
      android:layout_below="@+id/register_as_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <RadioButton 
      android:layout_marginStart="20dp" 
      android:textSize="22sp" 
      android:textColor="#141526" 
      android:id="@+id/contractor_radioBtn" 
      android:text="Contractor" 
      android:layout_marginTop="18dp" 
      android:layout_below="@+id/viewer_radioBtn" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <Button 

      android:id="@+id/continueBtn" 
      android:textSize="18sp" 
      android:text="CONTINUE" 
      android:textColor="#fff" 
      android:layout_marginTop="25dp" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@+id/contractor_radioBtn" 
      android:background="@drawable/ripple_maroon" 
      android:layout_width="250dp" 
      android:layout_height="38dp" /> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 


</RelativeLayout> 

Hier ist meine Java:

package com.devteam.abire.abire; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class b extends AppCompatActivity { 

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

Antwort

0

Wenn Sie klickbare zu einem Zeitpunkt nur eine der Schaltflächen zu machen, sollten Sie Radiobuttons innerhalb Radiogroup setzen

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
     <RadioButton 
      android:layout_marginStart="20dp" 
      android:textSize="22sp" 
      android:textColor="#141526" 
      android:id="@+id/viewer_radioBtn" 
      android:onCLick="onRadioButtonClicked" 
      android:text="Viewer" 
      android:layout_marginTop="18dp" 
      android:layout_below="@+id/register_as_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <RadioButton 
      android:layout_marginStart="20dp" 
      android:textSize="22sp" 
      android:textColor="#141526" 
      android:id="@+id/contractor_radioBtn" 
      android:onCLick="onRadioButtonClicked" 
      android:text="Contractor" 
      android:layout_marginTop="18dp" 
      android:layout_below="@+id/viewer_radioBtn" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
</RadioGroup> 

<Button 
      android:id="@+id/continueBtn" 
      android:textSize="18sp" 
      android:text="CONTINUE" 
      android:textColor="#fff" 
      android:layout_marginTop="25dp" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@+id/contractor_radioBtn" 
      android:onCLick="onButtonClicked" 
      android:background="@drawable/ripple_maroon" 
      android:layout_width="250dp" 
      android:layout_height="38dp" /> 

    package com.devteam.abire.abire; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 

    public class b extends AppCompatActivity { 

    private String mClickedRadioButton; 

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

    public void onRadioButtonClicked(View view) { 
     // Is the button now checked? 
      boolean checked = ((RadioButton) view).isChecked(); 

     // Check which radio button was clicked 
     switch(view.getId()) { 
      case R.id.viewer_radioBtn": 
        if (checked) 
         sClickedRadioButton = "viewer"; 
        break; 
       case R.id.contractor_radioBtn" 
        if (checked) 
         sClickedRadioButton; = "contractor";  
       break; 
      } 
    } 

    public void onButtonClicked(View v){ 

     if(sClickedRadioButton == null){ 
      return; 
     }else if(sClickedRadioButton.equals("viewer")){ 
      //Do something when view is checked 
     }else if(sClickedRadioButton.equals("contractor"){ 
      // Do something when contractor is checked 
     } 
} 
0

Erstens ist es gut, statt Radio-Taste zu verwenden Radiogroup, wenn Sie multipe Tasten haben.

Für "So aktivieren Sie eine Schaltfläche, wenn ein Optionsfeld ausgewählt ist. So deaktivieren Sie eine Schaltfläche, wenn kein Optionsfeld ausgewählt ist.", erstellen Sie Referenzen für beide Schaltflächen und Optionsfelder in Aktivität. Wenn dann erste Optionsfeld aktiviert ist, deaktivieren Taste durch:

button.setEnabled(false); 

Für „Schließlich, wie sowohl der Radiobuttons auf eine einzigartige Aktivität der Option abhängig gewählt senden Sie machen.“, Während der Verwendung Radiogroup Verwendung:

public void onCheckedChanged(RadioGroup arg0, int arg1) { 
    radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId();); 

if (radioButton.isChecked()) { 
    text=radioButton.getText().toString(); 
    if (text.equals("radiobtn1Option")) { 
    //TODO : start new activity 
    Intent intent = new Intent(this, YourNextSCreen.class); 
      startActivity(intent); 

    } if (text.equals("radiobtn2Option")) { 
    //TODO 
    } else { 
    //TODO 
    } 
} 

} });

+0

was bedeutet '.setEnabld' tun?, Hörte ich seine' .setEnabled' richtig? – W4R10CK

+0

Ja, es war ein Tippfehler. Danke hat es geändert ..! –

Verwandte Themen