2017-01-26 65 views
0

我正在製作一個應用程序,它需要兩種類型的用戶。 「觀衆」和「承包商」。我爲每個選項製作了兩個單選按鈕。我想知道三件事: 如何選擇單選按鈕時激活按鈕。如何在沒有選中單選按鈕時取消激活按鈕。最後,如何使這兩個單選按鈕將您發送給一個獨特的活動,具體取決於所選的選項。例如,我選擇「承包商」,然後按按鈕繼續,它會發送給我一個獨特的佈局,連接到該單選按鈕。如何使單選按鈕控制按鈕

這裏是我的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> 

這裏是我的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); 
} 
} 

回答

0

如果你想一次只點擊一個按鈕,你應該把單選按鈕內RadioGroup中

<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

首先,這是件好事,如果你有multipe按鍵改用單選按鈕的RadioGroup中。

對於「當選擇一個單選按鈕,如何激活按鈕。如何滅活按鈕時,沒有單選按鈕選擇。」,創建活動中的兩個按鈕和單選按鈕的引用。然後,如果第一個單選按鈕被選中,禁止通過按鈕:

button.setEnabled(false); 

對於「最後,如何使兩個單選按鈕,送你到根據選擇的選項的唯一活動。」,同時使用radiogroup使用:

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

什麼'.setEnabld'呢?,我聽見了它的'.setEnabled'吧? – W4R10CK

+0

是的,這是一個錯字。謝謝改變它..! –