当前位置: 首页 > 知识库问答 >
问题:

将int可提取资源设置为onClick

金令秋
2023-03-14

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

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);
        }


    }
}

共有1个答案

步衡
2023-03-14

您可以使用以下方法:

getResources().getDrawable(R.drawable.drawable)

在res/drawable中设置您的drawable对象,通过上面的代码获取它,并将其放入您的对话框中。

如果您有多个可绘制的use 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;
}
 类似资料:
  • res\drawable\mylayerlistdrawable.xml ----------------------------------------------------------------------------------- 最终答案 在api 21之前的xml可提取资源中,我们不能使用?attr。由aapt在编译时创建的可提取资源。运行时用于动态连接的Attr资源。 解决方案是为

  • 我试图使用数据绑定将可提取的资源ID设置为ImageView的Android:src 下面是我的目标: 它根本不显示图像。我做错了什么? 顺便说一句,它完美地使用了标准方式:

  • 在最近升级了我的android工作室后,我不能再构建我的项目了。 重建、清理项目和使缓存无效都不起作用! 添加android.enableaapt2=false行并不能解决真正的问题,因此我宁愿找到问题的根源。 下面显示了我的gradle.build文件:

  • 我正在做一个生产线的模拟,它是通过Excel建立的。现在有一个,需要为每台机器自由设置工人班次,总共三个班次。 我的想法是创建三个不同的时间表,表示三个班次中的每一个,然后在ResourcePool中使用If语句设置每个班次的使用。如图所示,在此处输入图像描述 提前致谢

  • 问题内容: 我有一个将Android数据保存在其中的函数,但必须将数据转换为。 只要是我想保存为 以下是我的代码,只要值是 将上述转化为。 这是我决定要执行的操作,但是仍然无法将字符串值转换为每当它的。 然后功能 我应该如何对其进行更改以使其起作用? 问题答案: 不用编写自己的函数,而是使用try-catch的内部构造。您的问题是,or 或值本身是a,并且您在null引用上调用方法。请尝试以下操作

  • 问题内容: 我有一个只接受字符串的方法。 但是只接受一个int。通常,这是由SWT.TOP是静态int之类的设置的。 是否可以用类似的方法调用此方法? 问题答案: 如果使用Java 7,则始终可以在字符串上使用: 说实话,我会避免使用像这样的字符串。如果我真的不得不以其他方式存储对齐状态,我将使用可能在较旧版本的Java中使用的枚举。