[英]setting int drawable resource to onClick
I am currently working on a quiz app of 10 questions whereby i need to display 5 images at selected question fields an alert dialog as a hint when a text is clicked. 我目前正在研究10个问题的测验应用程序,因此我需要在选定问题字段中显示5张图像,并在单击文本时显示提示对话框作为提示。 I have set drawable resource files to display each individual image to referred text.
我设置了可绘制资源文件,以将每个单独的图像显示为引用的文本。 How do i properly set the resource drawable int to the text when clicked?
单击时如何正确将资源可绘制int设置为文本? For now, am getting an error.
目前,出现错误。
QuestionLibrary.java QuestionLibrary.java
// This file contains questions from QuestionBank
class QuestionLibrary {
// array of questions
private String mQuestions [] = {
"Question1",
"Question2",
"Question3",
"Question4?",
"Qustion5",
"Question6",
"Question7",
"Question8",
"Question9",
"Question10"
};
// array of multiple choices for each question
private String mChoices [][] = {
// choices goes here
};
//setting images
public static int[] image_drawables = new int[] {
R.drawable_01, R.drawable_02, R.drawable_03, R.drawable_04
};
// array of correct answers - in the same order as array of questions
private String mCorrectAnswers[] = {
Answers goes here
};
// text to show hint reference
private static String[] click_me_text = {
"", "", "Hint", "", "Hint", "", "", "Click me!", "Click me!", "", "",
};
// method returns click_me text from array textQuestions[] based on array index
String getTextHint(int a) {
return click_me_text[a];
}
// method returns number of questions
int getLength(){
return mQuestions.length;
}
// method returns question from array textQuestions[] based on array index
String getQuestion(int a) {
return mQuestions[a];
}
// method return a single multiple choice item for question based on array index,
// based on number of multiple choice item in the list - 1, 2, 3 or 4 as an argument
String getChoice(int index, int num) {
return mChoices[index][num-1];
}
// method returns correct answer for the question based on array index
String getCorrectAnswer(int a) {
return mCorrectAnswers[a];
}
}
MainActivity.java MainActivity.java
public class MainActivity extends AppCompatActivity {
private QuestionLibrary mQuestionLibrary = new QuestionLibrary();
//initializing buttons
Button button1;
Button button2;
Button button3;
Button button4;
TextView msingleQuestion;
TextView mQuestion; //current question to answer
TextView mClickMe; // text to be clicked which should display image
private int mQuestionNumber = 0; // current question number
private String mAnswer; // correct answer for question in mQuestionView
private int mquizNumber = 1;
private int mfailedQuestions = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_begineer);
// set textViews here
msingleQuestion = (TextView) findViewById(R.id.singleQuestion);
mQuestion = (TextView) findViewById(R.id.txtQuestion);
mClickMe = (TextView) findViewById(R.id.click_me);
button1 = (Button) findViewById(R.id.firstOption);
button2 = (Button) findViewById(R.id.secondOption);
button3 = (Button) findViewById(R.id.thirdOption);
button4 = (Button) findViewById(R.id.fourthOption);
updateQuestion(); //update question
// setting respective animations for the buttons
blinkClickMe();
}
private void blinkClickMe() {
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(60);
anim.setStartOffset(30);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
mClickMe.startAnimation(anim);
}
private void updateQuizNumber(int mquizNumber) {
msingleQuestion.setText("" + mquizNumber+"/"+mQuestionLibrary.getLength());
}
private void updateQuestion() {
// check if we are not outside array bounds for questions
if(mQuestionNumber<mQuestionLibrary.getLength() ){
// set text for hint and click_me
mClickMe.setText(mQuestionLibrary.getTextHint(mQuestionNumber));
// set the text for new question, and new 4 alternative to answer on four buttons
mQuestion.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
button1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
button2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
button3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
button4.setText(mQuestionLibrary.getChoice(mQuestionNumber,4));
mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
mQuestionNumber++;
}
else {
Toast.makeText(BeginnerActivity.this, "It was the last question!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MenuOptions.class);
// intent.putExtra("score", mScore); // pass the current score to the second screen
startActivity(intent);
}
}
public void onClick(View view) {
//all logic for all answers buttons in one method
final Button answer = (Button) view;
// if the answer is correct, increase the score
if (answer.getText().equals(mAnswer) ){
answer.setBackgroundColor(Color.GREEN);
// blink correct answer
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(60);
anim.setStartOffset(30);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.RELATIVE_TO_PARENT);
answer.startAnimation(anim);
}else {
answer.setBackgroundColor(Color.RED);
mfailedQuestions--;
updateLives(mfailedQuestions);
}
if (mQuestionNumber < mQuestionLibrary.getLength()) {
// once user answer the question, we move on to the next one, if any
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
answer.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.rounded_button_questions));
}
updateQuestion();
mquizNumber++;
updateQuizNumber(mquizNumber);
}
}, 1000);
} else {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(BeginnerActivity.this, MenuOptions.class);
// intent.putExtra("score", mScore); // pass the current score to the second screen
startActivity(intent);
}
}, 1000);
}
}
public void revealImage(View view) {
loadImage();
}
private void loadImage() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
// what is needed here?
View dialogView = inflater.inflate(QuestionLibrary.image_drawables[mQuestionNumber], null);
builder.setView(dialogView)
.create().show();
}
@Override
public void onFinish() {
timer.setText("time's up!!");
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(60);
anim.setStartOffset(30);
anim.setRepeatMode(Animation.INFINITE);
anim.setRepeatCount(Animation.RELATIVE_TO_PARENT);
timer.startAnimation(anim);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 500);
}
}
}
Error FATAL EXCEPTION: main
错误致命异常:主要
Process: com.example.benkeys.pianoquizgame, PID: 3961 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.pe进程:com.example.benkeys.pianoquizgame,PID:3961 java.lang.IllegalStateException:无法在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)上执行android:onClick的方法android.os.Handler.handleCallback(Handler.java:751)上的android.view.View $ PerformClick.run(View.java:22429)上的.view.View.performClick(View.java:5637)。 java.lang.reflect.Method.invoke上的Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main(ActivityThread.java:6119) (原生方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)造成原因:java.lang android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)上的java.lang.reflect.Method.invoke(本机方法)处的.reflect.InvocationTargetException rformClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: android.content.res.Resources$NotFoundException: File res/drawable-xxhdpi-v4/beginner_04.png from xml type layout resource ID #0x7f060056 at android.content.res.ResourcesImpl.loadXmlResourceParser(ResourcesImpl.java:990) at android.content.res.Resources.loadXmlResourceParser(Resources.java:2103) at android.content.res.Resources.getLayout(Resources.java:1115) at android.view.LayoutInflater.inflate(LayoutInflater.java:424) at android.view.LayoutInflater.inflate(LayoutInflater.java:377
android.os.Handler.dispatchMessage(Handler)上的rformClick(View.java:5637)android.view.View $ PerformClick.run(View.java:22429)android.os.Handler.handleCallback(Handler.java:751) .java:95),位于android.os.Looper.loop(Looper.java:154),位于android.app.ActivityThread.main(ActivityThread.java:6119),位于java.lang.reflect.Method.invoke(本机方法),位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)原因:android.content.res.Resources $ NotFoundException:来自xml类型布局资源ID#0x7f060056的xml / layable-xxhdpi-v4 / beginner_04.png位于android.content.res.ResourcesImpl.loadXmlResourceParser(ResourcesImpl.java:990)位于android.content.res.Resources.loadXmlResourceParser( android.content.res.Resources.getLayout(Resources.java:1115)上的Resources.java:2103)android.view.LayoutInflater.inflate(LayoutInflater.java)上android.view.LayoutInflater.inflate(LayoutInflater.java:424)上的Resources.java:2103) :377 ) at com.example.benkeys.pianoquizgame.BeginnerActivity.loadImage(BeginnerActivity.java:244) at com.example.benkeys.pianoquizgame.BeginnerActivity.revealImage(BeginnerActivity.java:237) at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.io.FileNotFoundException: Corrupt XML binary file at android.content.res.AssetManager.openXmlAssetNative(Nati
)的com.example.benkeys.pianoquizgame.BeginnerActivity.loadImage(BeginnerActivity.java:244)的com.example.benkeys.pianoquizgame.BeginnerActivity.revealImage(BeginnerActivity.java:237)的java.lang.reflect.Method.invoke( android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)上的android.view.View.performClick(View.java:5637)上的android.view.View $ PerformClick.run( View.java:22429),位于android.os.Handler.handleCallback(Handler.java:751),位于android.os.Handler.dispatchMessage(Handler.java:95),位于android.os.Looper.loop(Looper.java:154) )的android.app.ActivityThread.main(ActivityThread.java:6119)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886的java.lang.reflect.Method.invoke(本机方法) )在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)造成原因:java.io.FileNotFoundException:android.content.res.AssetManager.openXmlAssetNative(Nati ve Method)
ve方法)
You can use this approach : 您可以使用这种方法:
getResources().getDrawable(R.drawable.drawable)
set your drawable object in res/Drawable and get it by code above and put in your dialog box. 在res / Drawable中设置您的可绘制对象,并通过上面的代码获取它,并将其放在对话框中。
if you have more than one drawable use switch() method like this: 如果您有多个可绘制对象,请使用switch()方法,如下所示:
switch(answeredQuestion)
{
case 1:
//show considered dialog box
break;
case 2:
// show considered dialog box
break;
...
case 10:
//show considered dialog box
default
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.