如何在Android上动态添加图片的图片资源?

如何在Android上动态添加图片的图片资源?,android,image,Android,Image,我有点自大。首先,我会发布截图,然后问我的问题 您可以看到一些椭圆形被选中。我所做的是维护两组图像,当用户单击时,白色图像变成黑色

我有点自大。首先,我会发布截图,然后问我的问题


您可以看到一些椭圆形被选中。我所做的是维护两组图像,当用户单击时,白色图像变成黑色<现在,我需要从这张答题纸上收集答案,并想知道如何收集答案。我尝试了一个名为
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;
    }
}
}
公共类QuizaActivity扩展活动实现OnClickListener{
/**在首次创建活动时调用*/
相对寿命r2;
//全局变量
int[][]quizData;//将测验规范存储在整数数组中
int[][]问题图像={
{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_选中,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_checked},
{R.drawable.zero,R.drawable.one,R.drawable.two,
拉深,三,四,五,
拉深,六,七,八,
R.drawable.nine,R.drawable.decimal},
{R.drawable.zero_选中,R.drawable.one_选中,
可拉拔,两个勾选,可拉拔,三个勾选,
R.可抽出式。四张支票,R.可抽出式。五张支票,
可拉拔,六个方格,可拉拔,七个方格,
R.可抽出式,八个方格,R.可抽出式,九个方格,
R.drawable.decimal_checked};
//结束
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//变量声明的开始
ScrollView s1=新的ScrollView(此);
RelativeLayout r1=新的RelativeLayout(本);
r2=r1;
File quizSpecs=new File(“mnt/sdcard/teacher_1.csv”);//读取文件
BufferedReader csvReader=null;
String line=”“;//将每一行存储在一个字符串中
StringTokenizer currentLine=null;
int noOfQuestions=0;//测验中的问题数
int time=0;//测验的持续时间
int i=0,j=0,k=0;//循环变量
int-previd=0;
//变量声明的结尾
试一试{
csvReader=newbufferedreader(newfilereader(quizSpecs));
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
line=csvReader.readLine();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
currentLine=新的StringTokenizer(行“,”);
noOfQuestions=Integer.parseInt(currentLine.nextToken());
time=Integer.parseInt(currentLine.nextToken());
//System.out.println(noOfQuestions+“”+时间);
while(currentLine.hasMoreTokens()){
currentLine.nextToken();
}
quizData=新整数[noOfQuestions][6];
对于(i=0;i=quizData[i][2]){
*选项.setVisibility(视图.不可见);
*option.setEnabled(false);}
*/
RelativeLayout.LayoutParams params=新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
参数addRule(RelativeLayout.down,((1000*i)+1));
参数addRule(相对长度右),
((1000*(i+1))+j));
previd=((1000*(i+1))+j);
option.setLayoutParams(参数);
r1.添加视图(选项,参数);
}
打破
案例3:
对于(j=0;j<(quizData[i][3]==0?quizData[i][2]
+quizData[i][3]:quizData[i][2]+quizData[i][3]+1;j++){
对于(k=10;k>-1;k--){
ImageView num=新的ImageView(此);
setImageResource(questionImages[2][10-k]);
数值setId((1000*(i+1))+(100*j)+k+1);
<?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 
        android:layout_height="30dp"
        android:layout_width="30dp"
        android:button="@drawable/bt"
        android:focusable="false"
        android:id="@+id/ch"/>
 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);
 }