简体   繁体   English

如何从onClickListener之外的onClickListener获取值

[英]How to get values from onClickListener outside of onClickListener

So I am trying to create a multiple choice quiz, that will generate a fitness program based on your choices. 因此,我正在尝试创建一个多项选择测验,根据您的选择生成健身计划。 I have three multiple choice buttons, and each time a question is answered, the question and the answers are rotated. 我有三个多选按钮,每次回答问题时,问题和答案都会轮换。 I am trying to grab the answer that the user selected for each question, however my program is not allowing me to correctly grab the data. 我试图抓住用户为每个问题选择的答案,但是我的程序不允许我正确地抓取数据。

I have tried using getters and setters but my program still cannot grab the information 我尝试过使用getter和setter但我的程序仍然无法获取信息

    private TextView blank;
    private Button mButtonChoice1;
    private Button mButtonChoice2;
    private Button mButtonChoice3;

    String experience;
    String preference;
    int days;

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        blank = (TextView) findViewById(R.id.program);
        mButtonChoice1 = (Button)findViewById(R.id.choice1);
        mButtonChoice2 = (Button)findViewById(R.id.choice2);
        mButtonChoice3 = (Button)findViewById(R.id.choice3);


         mButtonChoice1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice1.getText() == "Two"){ 
//QUESTION 1 ANSWER_1
                    updateQuestion();
                    days = 2;


                }
                else if (mButtonChoice1.getText() == "0-6 months") { //QUESTION 2 ANSWER_1
                    updateQuestion();
                    experience = "0-6 months";

                }

            }




          mButtonChoice2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice2.getText() == "Three"){ //QUESTION 1 ANSWER_2
                    updateQuestion();
                    days = 3;

                }
                else if(mButtonChoice2.getText() == "6-18 months"){ //QUESTION 2 ANSWER_2
                    updateQuestion();
                    experience = "6-18 months";

                }
           }
       }



          mButtonChoice3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice3.getText() == "Four"){ //QUESTION 1 ANSWER_3
                    updateQuestion();
                    days = 4;

                }
                else if (mButtonChoice3.getText() == "1.5+ years") { //QUESTION 2 ANSWER_3
                    updateQuestion();
                    experience = "1.5+ years";

                }
           }
       }



//here i am just testing if my program is able to receive the data that was entered for each variable 
if (day == 2 && preference == "Strength" && experience == "0-6 months") {

    blank.setText("test working");

}
else {

blank.setText("Test not working");

}



}// oncreate





However, right when I start the program, before I even choose any of the choices, the test testVIew section already states "test not working". 但是,当我启动程序时,在我选择任何选项之前,测试testVIew部分已经指出“测试不工作”。

My program will be able to get the value of the variable if I place the if statement inside the onclickListener function, however, I need the if statement at the bottom because I need to account for all cases of each variable, and cannot put the if statement in the onclick methods. 如果我将if语句放在onclickListener函数中,我的程序将能够获取变量的值,但是,我需要在底部的if语句,因为我需要考虑每个变量的所有情况,并且不能把if onclick方法中的语句。

I WANT TO BE ABLE TO GET THE values of the variables preference,days,experience outside of the onClickListener 我想知道onClickListener之外的变量preference,days,experience

Try to: 尝试:

if (day == 2 || preference == "Strength" || experience == "0-6 months")

or 要么

if(day ==2 ){
    statements;
}
else if(preference == "Strength"){
    statements;
}
else if(experience == "0-6 months"){
    statements;
}
else {
    blank.setText("Test not working");
}

In your onCreate, you juste set the listeners in the button, when you check the answers, the activity is still not displayed on screen. 在你的onCreate中,你可以在按钮中设置监听器,当你检查答案时,活动仍然没有显示在屏幕上。 the activity do the checks before you can see the buttons etc because the checks are in onCreate. 在您看到按钮等之前,活动会进行检查,因为检查在onCreate中。

You should do the checks after the buttons have been pressed. 按下按钮后应该进行检查。 For instance, in your updateQuestion() you detect that the last question has been answered and you make the checks and display the result at that time. 例如,在updateQuestion()您检测到最后一个问题已被回答,并且您在那时进行检查并显示结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM