[英]RadioGroup (Radio Button) will be automatically selected
I have in my App 10 multiple choice questions with radio buttons. 我的App 10中有带单选按钮的多项选择题。 When I now select no answer and go to the next question the app save the first value as selected and execute setOnCheckedChangeListener. 现在,当我没有选择答案并转到下一个问题时,该应用会将第一个值保存为所选值并执行setOnCheckedChangeListener。
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 File 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>
Use clearCheck() to uncheck all buttons. 使用clearCheck()取消选中所有按钮。
Clears the selection. 清除选择。 When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null. 清除选择后,该组中没有选中任何单选按钮,并且getCheckedRadioButtonId()返回null。
and write a method doSomething() if none of the radiobuttons is checked. 如果未选中任何单选按钮,则编写方法doSomething()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.