画像を画面に動的に追加するプロジェクトを行っています。すべてが動的に生成されるため、コンパイル時に作成されるXML参照はまったくありません。
全体が動的に生成されます。ここで、各画像にonclickイベントを追加する方法を知りたいと思います。基本的に、画像をクリックすると、濃い色の別の画像が表示されます(基本的には解答用紙なので、泡が影になっているように見えます)。このコンテキストでいくつかのサンプルコードが必要です。私はAndroidを初めて使用し、今ではほとんど迷っています。
助けてください!
PSソースコードが必要な場合は、喜んで提供します。私にお知らせください。
編集:コードを立てる:
package ekalavya.pratnala.quiz;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class QuizActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Beginning of variable declarations
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[][] quizData; // Storing the quiz specifications in an integer array
int i = 0, j = 0, k = 0; // Loop variables
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 } };
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();
}
ScrollView s1 = new ScrollView(this);
RelativeLayout r1 = new RelativeLayout(this);
for (i = 0; i < noOfQuestions; i++) {
System.out.println(quizData[i][2]);
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 < 10; j++) {
ImageView option = new ImageView(this);
option.setImageResource(questionImages[0][j]);
option.setId((1000 * (i + 1)) + j + 1);
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);
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);
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);
}
}