具有单选按钮的多个无线电组检查真/假方法

时间:2015-10-14 22:55:29

标签: android methods radio-button radio-group

我正在制作一个测验应用程序,LinearLayout(vert)和ScrollView以及许多布局(h-200dp,w-match parent)。每个布局都包含左侧的图片,右侧有一个RadioGroup,其中3个RadioButtons带有答案,还有一个ImageView字段位于RadioGroup下方。 (例如:有3个提供答案的城市的图片,只有一个是正确的)

我试图编写一个方法来获取那些RadioButtons的ID,然后点击识别是真还是假。实际上我正在考虑两种方法,一种用于真实,一种用于错误答案。

我还需要在下面的小字段中发布图像,具体取决于答案,是真(第一张图片)还是假(第二张图片)。这意味着我的方法必须为每个问题(内部布局)获取该ImageView字段的ID,并发布两个图像中的一个。

有谁知道处理这种情况的最佳方法是什么?由于这是我第一个真正想发布的应用程序,因此从更有经验的人那里获得帮助对我来说意义重大。 在此先感谢:)

这里有一些代码已完成。 我的方式需要太多的硬编码,想象一下,如果我想连续有40个问题。所以我试图让它变得更简单

布局(仅显示一个布局以缩短显示的鳕鱼):

<?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"
    android:background="@mipmap/rain">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView" >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="200dp">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="150dp"
                        android:layout_height="match_parent"
                        android:weightSum="1">

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/imageView007"
                            android:src="@mipmap/opasnost_na_putu"
                            android:layout_weight="0.68" />
                    </LinearLayout>

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_margin="10dp">

                        <RadioGroup
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:id="@+id/rg_opasnost_na_putu">

                            <RadioButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Opasnost na putu"
                                android:id="@+id/radioButton" />

                            <RadioButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Opasna krivina"
                                android:id="@+id/radioButton2" />

                            <RadioButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Suzenje puta"
                                android:id="@+id/radioButton3" />
                        </RadioGroup>

                        <ImageView
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:id="@+id/imageView37"
                            android:layout_gravity="center"
                            android:layout_marginTop="10dp" />

                    </LinearLayout>

                </LinearLayout>
                     ...
                     ...
                     ...
                    </LinearLayout>
               </ScrollView>
          </LinearLayout>

代码(也只有一个广播组在代码中显示缩短它):

public class Znaci_IN_Activity extends Activity {
// Points won
public int osvojeniPoeni = 0;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.znaci_in_layout);
// My Radio Group 
 RadioGroup rgDvostrukaKrivinaD = (RadioGroup) findViewById(R.id.rg_dvostruka_krivina_d);

// ImageView field for posting image if correct/incorrect answer
ImageView iv2 = (ImageView) findViewById(R.id.imageView007);

rgDvostrukaKrivinaD.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radioButton4:
                        osvojeniPoeni-=1;
                        iv2.setImageResource(R.mipmap.incorrect);
                        break;
                    case R.id.radioButton5:
                        osvojeniPoeni+=2;
                        iv2.setImageResource(R.mipmap.correct);
                        break;
                    case R.id.radioButton6:
                        osvojeniPoeni-=1;
                        iv2.setImageResource(R.mipmap.incorrect);
                        break;
                }
            }
        });

//Button with a TextView area to show points won on the end of the quiz
        Button osv_poeni = (Button) findViewById(R.id.osv_poeni);
        final TextView rez = (TextView) findViewById(R.id.rez);
        rez.setText(Integer.toString(osvojeniPoeni));
    }
}

0 个答案:

没有答案
相关问题