2014-09-18 86 views
0

我在我的App 10中有單選按鈕的多選題。 當我現在選擇無回答並轉到下一個問題時,應用程序將第一個值保存爲選定的值並執行setOnCheckedChangeListener。RadioGroup(單選按鈕)將被自動選中

protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_questions); 

    sHelper = new SystemHelper(getApplicationContext()); 

    db = new databaseHandler(getApplicationContext()); 
    user = new UserClass(getApplicationContext()); 
    user.makeUserFromShared(); 

    osGroup = (RadioGroup) findViewById(R.id.osGroup); 
    question = (TextView) findViewById(R.id.question); 
    questionNr = (TextView) findViewById(R.id.questionNr); 
    left = (TextView) findViewById(R.id.left); 
    right = (TextView) findViewById(R.id.right); 
    scroll = (ScrollView) findViewById(R.id.scroll); 
    one = (RadioButton) findViewById(R.id.one); 
    two = (RadioButton) findViewById(R.id.two); 
    three = (RadioButton) findViewById(R.id.three); 
    four = (RadioButton) findViewById(R.id.four); 


    osGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      Log.d("Save",String.valueOf(checkedId)); 
      switch (checkedId){ 
       case -1: 
        break; 
       case R.id.one: 
        setAnswer(0); 
        break; 
       case R.id.two: 
        setAnswer(1); 
        break; 
       case R.id.three: 
        setAnswer(2); 
        break; 
       case R.id.four: 
        setAnswer(3); 
        break; 
       default: 
        break; 
      } 
     } 
    }); 
} 

public void setAnswer(int aPos){ 
    int qPos = ((globals) this.getApplication()).getQesNr(); 
    classQuestion quest = questions.get(qPos); 
    ArrayList<classAnswer> answers = db.getAnswers(quest.getFbid()); 
    questions.get(((globals) getApplication()).getQesNr()).setAnswer(answers.get(aPos).getFbid()); 
    db.saveAnswer(quest.getFbid(),answers.get(aPos).getFbid()); 
} 

public void goRight(View view){ 
    int pos = ((globals) this.getApplication()).getQesNr() + 1; 
    ((globals) this.getApplication()).setQesNr(pos); 
    setQuestion(questions); 
} 

public void goLeft(View view){ 
    int pos = ((globals) this.getApplication()).getQesNr() - 1; 
    ((globals) this.getApplication()).setQesNr(pos); 
    setQuestion(questions); 
} 

public void goRightSwipe(){ 
    if(((globals) this.getApplication()).getQesNr() < 9){ 
     int pos = ((globals) this.getApplication()).getQesNr() + 1; 
     ((globals) this.getApplication()).setQesNr(pos); 
     setQuestion(questions); 
    } 
} 

public void goLeftSwipe(){ 
    if(((globals) this.getApplication()).getQesNr() != 0){ 
     int pos = ((globals) this.getApplication()).getQesNr() - 1; 
     ((globals) this.getApplication()).setQesNr(pos); 
     setQuestion(questions); 
    } 
} 

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="wrap_content" 
    tools:context="de.skverlag.fortbildungrettungsdienst.questions" 
    android:padding="10dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="24sp" 
     android:text="" 
     android:id="@+id/questionNr" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerVertical="true" 
     android:layout_marginBottom="10dp" 
     android:textColor="@color/white" 
     android:gravity="center"/> 

    <TextView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:textSize="24sp" 
     android:text="@string/left" 
     android:id="@+id/left" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerVertical="true" 
     android:textColor="@color/white" 
     android:clickable="true" 
     android:onClick="goLeft" 
     /> 

    <TextView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:textSize="24sp" 
     android:text="@string/right" 
     android:id="@+id/right" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerVertical="true" 
     android:textColor="@color/white" 
     android:clickable="true" 
     android:onClick="goRight" 
     android:gravity="right"/> 

    <View 
     android:id="@+id/seperator" 
     android:layout_width="fill_parent" 
     android:layout_height="1px" 
     android:background="@android:color/background_light" 
     android:layout_below="@id/questionNr"/> 

    <ScrollView 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/seperator" 
     android:layout_marginTop="10dp"> 

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

      <TextView 
       android:id="@+id/question" 
       android:text="" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:textColor="@color/white"/> 

      <RadioGroup 
       android:id="@+id/osGroup" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/question" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_marginTop="20dp"> 

       <RadioButton 
        android:id="@+id/one" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="" 
        android:textSize="16sp" 
        android:padding="7dp" 
        android:textColor="@color/white" 
        android:layout_marginTop="20dp" 
        android:background="@color/blacktrans"/> 

       <RadioButton 
        android:id="@+id/two" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="7dp" 
        android:text="" 
        android:textSize="16sp" 
        android:textColor="@color/white" 
        android:background="@color/blacktrans" 
        android:layout_marginTop="20dp" /> 

       <RadioButton 
        android:id="@+id/three" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="7dp" 
        android:text="" 
        android:textSize="16sp" 
        android:textColor="@color/white" 
        android:background="@color/blacktrans" 
        android:layout_marginTop="20dp" /> 

       <RadioButton 
        android:id="@+id/four" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="7dp" 
        android:text="" 
        android:textSize="16sp" 
        android:textColor="@color/white" 
        android:background="@color/blacktrans" 
        android:layout_marginTop="20dp" /> 
      </RadioGroup> 
     </RelativeLayout> 
    </ScrollView> 
</RelativeLayout> 
+1

是的。你怎麼了?請證明一個[MCV](http://stackoverflow.com/help/mcve)的例子! – 2014-09-18 16:42:33

回答

0

使用clearCheck()取消選中所有按鈕。

清除選擇。當選擇被清除時,該組中沒有選擇單選按鈕,並且getCheckedRadioButtonId()返回null。

並編寫doSomething()方法,如果沒有檢查任何單選按鈕。

相關問題