0

我有点卡住了。首先,我将发布屏幕截图,然后问我的问题。

输出


您可以看到一些椭圆被选中。我所做的是维护 2 组图像,当用户单击时,白色图像变为黑色。
现在,我需要从这个答题卡中收集答案,并想知道如何。我尝试了一种名为getImageResource()但不存在的方法。请帮助我如何去做。

代码:

 public class QuizActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
RelativeLayout r2;
// Global variable(s)
int[][] quizData; // Storing the quiz specifications in an integer array
int[][] questionImages = {
        { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
                R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h,
                R.drawable.i, R.drawable.j },
        { R.drawable.a_checked, R.drawable.b_checked, R.drawable.c_checked,
                R.drawable.d_checked, R.drawable.e_checked,
                R.drawable.f_checked, R.drawable.g_checked,
                R.drawable.h_checked, R.drawable.i_checked,
                R.drawable.j_checked },
        { R.drawable.zero, R.drawable.one, R.drawable.two,
                R.drawable.three, R.drawable.four, R.drawable.five,
                R.drawable.six, R.drawable.seven, R.drawable.eight,
                R.drawable.nine, R.drawable.decimal },
        { R.drawable.zero_checked, R.drawable.one_checked,
                R.drawable.two_checked, R.drawable.three_checked,
                R.drawable.four_checked, R.drawable.five_checked,
                R.drawable.six_checked, R.drawable.seven_checked,
                R.drawable.eight_checked, R.drawable.nine_checked,
                R.drawable.decimal_checked } };

// End
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Beginning of variable declarations
    ScrollView s1 = new ScrollView(this);
    RelativeLayout r1 = new RelativeLayout(this);
    r2 = r1;
    File quizSpecs = new File("mnt/sdcard/teacher_1.csv"); // Read the file
    BufferedReader csvReader = null;
    String line = ""; // Storing each line in a string
    StringTokenizer currentLine = null;
    int noOfQuestions = 0; // Number of questions in the quiz
    int time = 0; // Duration of the quiz
    int i = 0, j = 0, k = 0; // Loop variables
    int previd = 0;
    // End of variable declarations

    try {
        csvReader = new BufferedReader(new FileReader(quizSpecs));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        line = csvReader.readLine();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    currentLine = new StringTokenizer(line, ",");
    noOfQuestions = Integer.parseInt(currentLine.nextToken());
    time = Integer.parseInt(currentLine.nextToken());
    // System.out.println(noOfQuestions + " " + time);
    while (currentLine.hasMoreTokens()) {
        currentLine.nextToken();
    }
    quizData = new int[noOfQuestions][6];
    for (i = 0; i < noOfQuestions; i++) {
        try {
            line = csvReader.readLine();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        currentLine = new StringTokenizer(line, ",");
        for (j = 0; j < 6; j++) {
            quizData[i][j] = Integer.parseInt(currentLine.nextToken());
            // System.out.println(quizData[i][j]);
        }
    }
    try {
        csvReader.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for (i = 0; i < noOfQuestions; i++) {
        TextView questionNo = new TextView(this);
        questionNo.setText(String.valueOf(i + 1));
        questionNo.setId(1000 * (i + 1));
        questionNo.setTextSize(18);
        RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        p1.addRule(RelativeLayout.BELOW, previd);
        previd = (1000 * (i + 1));
        questionNo.setLayoutParams(p1);
        r1.addView(questionNo, p1);
        switch (quizData[i][1]) {
        case 1:
        case 2:
            for (j = 0; j < quizData[i][2]; j++) {
                ImageView option = new ImageView(this);
                option.setImageResource(questionImages[0][j]);
                option.setId((1000 * (i + 1)) + j + 1);
                option.setOnClickListener(this);
                /*
                 * if (j >= quizData[i][2]) {
                 * option.setVisibility(View.INVISIBLE);
                 * option.setEnabled(false); }
                 */
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.BELOW, ((1000 * i) + 1));
                params.addRule(RelativeLayout.RIGHT_OF,
                        ((1000 * (i + 1)) + j));
                previd = ((1000 * (i + 1)) + j);
                option.setLayoutParams(params);
                r1.addView(option, params);
            }
            break;
        case 3:
            for (j = 0; j < (quizData[i][3] == 0 ? quizData[i][2]
                    + quizData[i][3] : quizData[i][2] + quizData[i][3] + 1); j++) {
                for (k = 10; k > -1; k--) {
                    ImageView num = new ImageView(this);
                    num.setImageResource(questionImages[2][10 - k]);
                    num.setId((1000 * (i + 1)) + (100 * j) + k + 1);
                    num.setOnClickListener(this);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
                    if (j == 0) {
                        params.addRule(RelativeLayout.RIGHT_OF,
                                (1000 * (i + 1)));
                    } else {
                        params.addRule(RelativeLayout.RIGHT_OF,
                                (1000 * (i + 1)) + (100 * (j - 1)) + k + 1);
                    }
                    if (k == 10) {
                        params.addRule(RelativeLayout.BELOW, (1000 * i) + 1);
                    } else {
                        params.addRule(RelativeLayout.BELOW,
                                ((1000 * (i + 1)) + (100 * j) + k + 2));
                    }
                    num.setLayoutParams(params);
                    r1.addView(num, params);
                }
            }
            previd = (1000 * (i + 1)) + 1;
            break;
        case 4:
        case 5:
            for (j = quizData[i][2] - 1; j > -1; j--) {
                for (k = 0; k < quizData[i][3]; k++) {
                    ImageView match = new ImageView(this);
                    match.setImageResource(questionImages[0][k]);
                    match.setId((1000 * (i + 1)) + (100 * j) + k + 1);
                    match.setOnClickListener(this);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
                    if (k == 0) {
                        params.addRule(RelativeLayout.RIGHT_OF,
                                (1000 * (i + 1)));
                    } else {
                        params.addRule(RelativeLayout.RIGHT_OF,
                                (1000 * (i + 1)) + (100 * j) + k);
                    }
                    if (j == quizData[i][2] - 1) {
                        params.addRule(RelativeLayout.BELOW, (1000 * i) + 1);
                    } else {
                        params.addRule(RelativeLayout.BELOW,
                                (1000 * (i + 1)) + (100 * (j + 1)) + k + 1);
                    }
                    match.setLayoutParams(params);
                    r1.addView(match, params);
                }
            }
            previd = (1000 * (i + 1)) + 1;
            break;
        }
    }
    s1.addView(r1, new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    this.setContentView(s1);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    // Beginning of variable declarations
    int clickedButton = v.getId();
    int questionNo = clickedButton / 1000; // Finding the question number
    int i = 0; // Loop variable
    int rowNo = (clickedButton / 100) % 10;
    // System.out.println(questionNo);
    // System.out.println(quizData[questionNo - 1][1]);
    switch (quizData[questionNo - 1][1]) {
    case 1:
        for (i = 0; i < quizData[questionNo - 1][2]; i++) {
            ImageView option = new ImageView(this);
            option.setImageResource(questionImages[0][i]);
            option.setId((1000 * questionNo) + i + 1);
            option.setOnClickListener(this);
            /*
             * if (i >= quizData[questionNo - 1][2]) {
             * option.setVisibility(View.INVISIBLE);
             * option.setEnabled(false); }
             */
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.BELOW,
                    ((1000 * (questionNo - 1)) + 1));
            params.addRule(RelativeLayout.RIGHT_OF,
                    ((1000 * questionNo) + i));
            option.setLayoutParams(params);
            r2.addView(option, params);
        }
        ImageView option = new ImageView(this);
        option.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
        option.setId((1000 * questionNo) + (clickedButton % 10));
        option.setOnClickListener(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW,
                ((1000 * (questionNo - 1)) + 1));
        params.addRule(RelativeLayout.RIGHT_OF, ((1000 * questionNo)
                + (clickedButton % 10) - 1));
        option.setLayoutParams(params);
        r2.addView(option, params);
        break;
    case 2:
        ImageView checked = new ImageView(this);
        checked.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
        checked.setId((1000 * questionNo) + (clickedButton % 10));
        checked.setOnClickListener(this);
        RelativeLayout.LayoutParams params_checked = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params_checked.addRule(RelativeLayout.BELOW,
                ((1000 * (questionNo - 1)) + 1));
        params_checked.addRule(RelativeLayout.RIGHT_OF,
                ((1000 * questionNo) + (clickedButton % 10) - 1));
        checked.setLayoutParams(params_checked);
        r2.addView(checked, params_checked);
        break;
    case 3:
        for (i = 10; i > -1; i--) {
            ImageView num = new ImageView(this);
            num.setImageResource(questionImages[2][10 - i]);
            num.setId((1000 * questionNo) + (100 * rowNo) + i + 1);
            num.setOnClickListener(this);
            RelativeLayout.LayoutParams params_num = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            if (rowNo == 0) {
                params_num.addRule(RelativeLayout.RIGHT_OF,
                        (1000 * questionNo));
            } else {
                params_num.addRule(RelativeLayout.RIGHT_OF,
                        (1000 * questionNo) + (100 * (rowNo - 1)) + i + 1);
            }
            if (i == 10) {
                params_num.addRule(RelativeLayout.BELOW,
                        (1000 * (questionNo - 1)) + 1);
            } else {
                params_num.addRule(RelativeLayout.BELOW,
                        ((1000 * questionNo) + (100 * rowNo) + i + 2));
            }
            num.setLayoutParams(params_num);
            r2.addView(num, params_num);
        }
        ImageView num = new ImageView(this);
        num.setImageResource(questionImages[3][11 - (clickedButton % 100)]);
        num.setId((1000 * questionNo) + (100 * rowNo)
                + (clickedButton % 100));
        num.setOnClickListener(this);
        RelativeLayout.LayoutParams params_num = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (rowNo == 0) {
            params_num
                    .addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo));
        } else {
            params_num.addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo)
                    + (100 * (rowNo - 1)) + (clickedButton % 100));
        }
        if (((clickedButton % 100) - 1) == 10) {
            params_num.addRule(RelativeLayout.BELOW,
                    (1000 * (questionNo - 1)) + 1);
        } else {
            params_num.addRule(RelativeLayout.BELOW, ((1000 * questionNo)
                    + (100 * rowNo) + (clickedButton % 100) + 1));
        }
        num.setLayoutParams(params_num);
        r2.addView(num, params_num);
        break;
    case 4:
        for (i = 0; i < quizData[questionNo - 1][3]; i++) {
            ImageView match = new ImageView(this);
            match.setImageResource(questionImages[0][i]);
            match.setId((1000 * questionNo) + (100 * rowNo) + i + 1);
            match.setOnClickListener(this);
            RelativeLayout.LayoutParams params_match = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            if (i == 0) {
                params_match.addRule(RelativeLayout.RIGHT_OF,
                        (1000 * questionNo));
            } else {
                params_match.addRule(RelativeLayout.RIGHT_OF,
                        (1000 * questionNo) + (100 * rowNo) + i);
            }
            if (rowNo == quizData[questionNo - 1][2] - 1) {
                params_match.addRule(RelativeLayout.BELOW,
                        (1000 * (questionNo - 1)) + 1);
            } else {
                params_match.addRule(RelativeLayout.BELOW,
                        (1000 * questionNo) + (100 * (rowNo + 1)) + i + 1);
            }
            match.setLayoutParams(params_match);
            r2.addView(match, params_match);
        }
        ImageView match = new ImageView(this);
        match.setImageResource(questionImages[1][(clickedButton % 10) - 1]);
        match.setId((1000 * questionNo) + (100 * rowNo)
                + (clickedButton % 10));
        match.setOnClickListener(this);
        RelativeLayout.LayoutParams params_match = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (((clickedButton % 10) - 1) == 0) {
            params_match.addRule(RelativeLayout.RIGHT_OF,
                    (1000 * questionNo));
        } else {
            params_match.addRule(RelativeLayout.RIGHT_OF,
                    (1000 * questionNo) + (100 * rowNo)
                            + (clickedButton % 10) - 1);
        }
        if (rowNo == quizData[questionNo - 1][2] - 1) {
            params_match.addRule(RelativeLayout.BELOW,
                    (1000 * (questionNo - 1)) + 1);
        } else {
            params_match.addRule(RelativeLayout.BELOW,
                    (1000 * (questionNo)) + (100 * (rowNo + 1))
                            + (clickedButton % 10));
        }
        match.setLayoutParams(params_match);
        r2.addView(match, params_match);
        break;
    case 5:
        ImageView match_checked = new ImageView(this);
        match_checked
                .setImageResource(questionImages[1][(clickedButton % 10) - 1]);
        match_checked.setId((1000 * questionNo) + (100 * rowNo)
                + (clickedButton % 10));
        match_checked.setOnClickListener(this);
        RelativeLayout.LayoutParams params_mc = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        if (((clickedButton % 10) - 1) == 0) {
            params_mc.addRule(RelativeLayout.RIGHT_OF, 1000 * questionNo);
        } else {
            params_mc.addRule(RelativeLayout.RIGHT_OF, (1000 * questionNo)
                    + (100 * rowNo) + (clickedButton % 10) - 1);
        }
        if (rowNo == quizData[questionNo - 1][2] - 1) {
            params_mc.addRule(RelativeLayout.BELOW,
                    (1000 * (questionNo - 1)) + 1);
        } else {
            params_mc.addRule(RelativeLayout.BELOW, (1000 * (questionNo))
                    + (100 * (rowNo + 1)) + (clickedButton % 10));
        }
        match_checked.setLayoutParams(params_mc);
        r2.addView(match_checked, params_mc);
        System.out.println(match_checked.getDrawable());
        break;
    }
}
}
4

2 回答 2

1

与其跟踪当前正在使用的图像,不如从数据模型的角度考虑问题。数据模型保存应用程序的状态。在我看来,您拥有的是一组问题,每个问题都有不同的可能答案,然后是选定的答案/答案。如果您对数据进行适当的建模,则可以将所需的任何类型的视图逻辑应用于模型的状态。这直接来自 MVC(模型视图控制器)范例。

我怀疑你的应用程序现在是否会扩展,因此你应该定义某种模型,这可以很容易地用几个类(问题类、答案类)来完成,你可以用几种不同的方式定义关系。从那里您只需根据模型的当前状态写出视图。在android中,这是通过AdaptersAdapterView完成的。通常,您不会直接使用这些,在您的情况下,您可能会使用带有ListAdapter的ListView。您要做的是当用户单击某些内容时,您将捕获该事件,然后更新模型。您的 ListAdapter 将通过DataSetObserver侦听模型的更改,但是您可以只收听点击并直接在 onclick 侦听器中进行更改,而不是像观察者一样走完全程。

除非您真正熟悉所有这些事情,否则这显然不是您可能在一两个小时内完成的事情,但 IMO 它将使您的应用程序更清洁、更容易调试、更容易测试和更健壮。

于 2012-06-18T06:01:47.213 回答
0

下面是名称为“bt.xml”的 CheckBox 的选择器,并将此文件放入您的可绘制文件夹

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:drawable="@drawable/chceked_image"/>
    <item android:drawable="@drawable/unchceked_image"/>

</selector>

并在布局文件夹中使用 CheckBox 而不是 ImageView,例如名称为“row.xml”

<CheckBox 
        android:layout_height="30dp"
        android:layout_width="30dp"
        android:button="@drawable/bt"
        android:focusable="false"
        android:id="@+id/ch"/>

按钮属性用自己的图片改变 Checkbox 的图片

如果您将使用 ChcekBox,它有两种状态已选中和未选中,那么您很容易获得答案状态。

现在使用布局充气器来充气 xml,例如你有任何问题的三个答案,

 ChcekBox ch[] = new CheckBox[3];

 layout = findViewById(R.id.main_layout);
 LayoutInflater layoutInflater = LayoutInflater.from(this);

 for(int i = 0; i<3; i++)
 {
   View rowView = layoutInflater.inflate(R.layout.row, layout);
   ch[i] = (CheckBox)rowView.findViewById(R.id.ch);
   ch[i].setId(assign_new_id);
 }
于 2012-06-18T07:33:39.323 回答