有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从onClickListener外部的onClickListener获取值

所以我正在尝试创建一个选择题测验,它将根据你的选择生成一个健身计划。我有三个多选按钮,每次回答一个问题时,问题和答案都会轮换。我试图获取用户为每个问题选择的答案,但是我的程序不允许我正确获取数据

我尝试过使用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





然而,就在我启动程序的时候,甚至在我选择任何选项之前,test-testVIew部分已经声明“test-not-working”

如果我将if语句放在onclickListener函数中,我的程序将能够获得变量的值,但是,我需要在底部使用if语句,因为我需要考虑每个变量的所有情况,并且不能将if语句放在onclick方法中

我希望能够在onClickListener之外获取变量preference,days,experience的值


共 (2) 个答案

  1. # 1 楼答案

    在onCreate中,您只需在按钮中设置侦听器,当您检查答案时,活动仍不会显示在屏幕上。活动在您看到按钮等之前进行检查,因为检查在onCreate中

    你应该在按下按钮后进行检查。例如,在你的updateQuestion()中,你检测到最后一个问题已经得到了回答,你进行了检查并显示了当时的结果

  2. # 2 楼答案

    尝试:

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

    或者

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