2015-10-27 5 views
0
<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" 
     android:paddingLeft="24dp" 
     android:text="1. Do you have an anti-virus installed on your computer :" 
     android:textAllCaps="false" /> 

    <RadioGroup 
     android:id="@+id/radio_virus" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="48dp"> 

     <RadioButton 
      android:id="@+id/radio_virusyes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:onClick="assessUserInput" 
      android:text="Yes" /> 

     <RadioButton 
      android:id="@+id/radio_virusno" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="assessUserInput" 
      android:text="No" /> 
    </RadioGroup> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="48dp" 
     android:text="If No, skip question 2" 
     android:textAllCaps="false" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" 
     android:paddingLeft="24dp" 
     android:text="2. How many anti-viruses do you use on your laptop :" 
     android:textAllCaps="false" /> 

    <RadioGroup 
     android:id="@+id/radio_avnumber" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="48dp"> 

     <RadioButton 
      android:id="@+id/radio_avnumberone" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="assessUserInput" 
      android:text="1" /> 

     <RadioButton 
      android:id="@+id/radio_avnumbermore" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="assessUserInput" 
      android:text="More than 1" /> 
    </RadioGroup> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" 
     android:paddingLeft="24dp" 
     android:text="3. Do you use pop-up/advert blockers:" 
     android:textAllCaps="false" /> 

    <RadioGroup 
     android:id="@+id/radio_adblock" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="48dp"> 

     <RadioButton 
      android:id="@+id/radio_adblockyes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:onClick="assessUserInput" 
      android:text="Yes" /> 

     <RadioButton 
      android:id="@+id/radio_adblockno" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="assessUserInput" 
      android:text="No" /> 
    </RadioGroup> 

Вместо написания текста для руководства пользователя, чтобы пропустить вопрос 2 после того, как он
выбирает «Нет» в вопросе 1, я хочу быть в состоянии найти способ отключить вопрос два с помощью Java или XML-кода, когда пользователь выбирает «Нет». или автоматически ответить на два вопроса.Как убедиться, что выбор радиокнопки из группы радиостанций влияет на выбор радиокнопки в другой группе радиостанций.

ответ

0

Деятельность: (Примечание: я не знаю, зачем вам строки, поэтому я отделил их от метода отключения, но при этом он отключит переключатели, если пользователь не нажал, и они будут включены, когда пользователь нажмет да)

public class main extends Activity 
{ 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.mainz); 
      RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_virus); 
      radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(RadioGroup group, int checkedId) { 
        RadioGroup rg1  = (RadioGroup) findViewById(R.id.radio_avnumber); 
        Log.w("","click!"); 
        if (checkedId == R.id.radio_virusno) { 
         Log.w("","asdasda"); 
         for (int i = 0; i < rg1.getChildCount(); i++) { 
          rg1.getChildAt(i).setEnabled(false); 
         } 

        }else 
        { 
         Log.w("","else"); 
         for (int i = 0; i < rg1.getChildCount(); i++) { 
          rg1.getChildAt(i).setEnabled(true); 
         } 

        } 
       } 
      }); 
     } 


     public void assessUserInput(View view){ 
      //// constructor method for Radio Group : Radio_virus 
      RadioGroup rgVirus=(RadioGroup)findViewById(R.id.radio_virus); 
      String radioVirus=((RadioButton)findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString(); 

      //// constructor method for Radio Group : Radio_avnumber 
      RadioGroup rgAvnNumber=(RadioGroup)findViewById(R.id.radio_avnumber); 
      String radioAvNumber=((RadioButton)findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString(); 

      /// constructor method for Radio Group : Radio_adblock 
      RadioGroup rgAdBlock=(RadioGroup)findViewById(R.id.radio_adblock); 
      String radioAdBlock=((RadioButton)findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString(); 

      // constructor method for Input field 
      //EditText nameTextField=(EditText)findViewById(R.id.edit_name); 
      //String hasName=nameTextField.getText().toString(); 

     } 
    } 

Б XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" 
     android:paddingLeft="24dp" 
     android:text="1. Do you have an anti-virus installed on your computer :" 
     android:textAllCaps="false" /> 

    <RadioGroup 
     android:id="@+id/radio_virus" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="48dp"> 

     <RadioButton 
      android:id="@+id/radio_virusyes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:text="Yes" /> 

     <RadioButton 
      android:id="@+id/radio_virusno" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="No" /> 
    </RadioGroup> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="48dp" 
     android:text="If No, skip question 2" 
     android:textAllCaps="false" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" 
     android:paddingLeft="24dp" 
     android:text="2. How many anti-viruses do you use on your laptop :" 
     android:textAllCaps="false" /> 

    <RadioGroup 
     android:id="@+id/radio_avnumber" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="48dp"> 

     <RadioButton 
      android:id="@+id/radio_avnumberone" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="1" /> 

     <RadioButton 
      android:id="@+id/radio_avnumbermore" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="More than 1" /> 
    </RadioGroup> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" 
     android:paddingLeft="24dp" 
     android:text="3. Do you use pop-up/advert blockers:" 
     android:textAllCaps="false" /> 

    <RadioGroup 
     android:id="@+id/radio_adblock" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="48dp"> 

     <RadioButton 
      android:id="@+id/radio_adblockyes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:text="Yes" /> 

     <RadioButton 
      android:id="@+id/radio_adblockno" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="No" /> 
    </RadioGroup> 
</LinearLayout> 
+0

Я также ценю ваше усилие. Но мне было трудно реализовать свое предложение непосредственно в моей работе. Я ценю ваш ответ – jaido

+0

Я не могу реализовать ваш код. с какой группой вы ссылаетесь в первой строке вашего кода ?. Радиогруппа отключит кнопки во второй радиогруппе? , – jaido

+0

Я только что опубликовал свою деятельность ниже, чтобы вы мне помогли ... я потерялся. – jaido

0

Вы должны обрабатывать OnClick вашего радиокнопки:

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

    // Check which radio button was clicked 
    switch(view.getId()) { 
     case R.id.radio_virusyes: 
       if (checked){ 
         for(int i = 0; i < radio_avnumber.getChildCount(); i++){ 
       ((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(true);}} 
      break; 
     case R.id.radio_virusno: 
      if (checked){ 
       for(int i = 0; i < radio_avnumber.getChildCount(); i++){ 
     ((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(false);} 
    } 
      break; 
    } 
} 
+0

Большое спасибо, я использовал ваш код, и он сделал именно то, что я хотел. Еще раз спасибо. Извините за более поздний ответ ... – jaido

+0

Нет проблем, удачи –

0
public void assessUserInput(View view) { 
    //// constructor method for Radio Group : Radio_virus 
    RadioGroup rgVirus = (RadioGroup) findViewById(R.id.radio_virus); 
    String radioVirus = ((RadioButton) findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString(); 

    //// constructor method for Radio Group : Radio_avnumber 
    RadioGroup rgAvnNumber = (RadioGroup) findViewById(R.id.radio_avnumber); 
    String radioAvNumber = ((RadioButton) findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString(); 

    /// constructor method for Radio Group : Radio_adblock 
    RadioGroup rgAdBlock = (RadioGroup) findViewById(R.id.radio_adblock); 
    String radioAdBlock = ((RadioButton) findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString(); 

    // constructor method for Input field 
    EditText nameTextField = (EditText) findViewById(R.id.edit_name); 
    String hasName = nameTextField.getText().toString(); 

    // Is the button now checked 

    radioVirus.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if (checkedId = radio_virusno) { 
      //set the style of the things you want to disable here 

      } 
     } 
    }); 
+0

Просьба уточнить, как этот код отвечает на вопрос. – JAL

+0

показывал «ivan» мой текущий код. Это не должно было быть решением. был в спешке. Прости... – jaido

Смежные вопросы