简体   繁体   English

如何在事件监听器的语句中调用函数?

[英]How do I call a function from within a statement in the event listener?

I've been working on a little project, and I had a problem in implementing my idea. 我一直在做一个小项目,但是在实现我的想法时遇到了问题。 The app I'm trying to build is an educational app that gives random basic math (+,-,*,/) question, if the student got it right, it should show the next question, otherwise, it should give him/her another chance to try again. 我尝试构建的应用程序是一个教育性应用程序,它会给出随机的基本数学(+,-,*,/)问题,如果学生正确回答,它应该显示下一个问题,否则,应该给他/她再次尝试的机会。

My problem falls specifically in the last part, I wrote a function to show the next question, but I don't exactly know how to call it when the button is clicked. 我的问题专门放在最后一部分,我编写了一个函数来显示下一个问题,但是我不完全知道单击按钮时如何调用它。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;

import java.util.Random;

import static java.lang.String.valueOf;


public class MainActivity extends AppCompatActivity {
    TextView    fN;
    TextView    sN;
    TextView    oP;
    TextView    out;
    TextView    ht;
    EditText    ans;
    Button      btn;
    int answer;
    int fNum;
    int sNum;
    int opNum;
    Random r;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ht.setVisibility(View.GONE);

        final String[]    ops    = {"+","-","÷","×"};
        final String[]    pass   = {
                                    "Very good!",
                                    "Excellent!",
                                    "Nice work!",
                                    "Keep up the good work!"

        };
        final String[]    fail   = {
                                    "No. Please try again.",
                                    "Wrong. Try once more.",
                                    "Don't give up!",
                                    "No. Keep trying."
        };
        public static void nextQ() {
            //UI variables
            fN = findViewById(R.id.fN);
            sN = findViewById(R.id.sN);
            oP = findViewById(R.id.oP);
            out = findViewById(R.id.out);
            ans = findViewById(R.id.Ans);
            btn = findViewById(R.id.btn);
            //functionalities variables
            r = new Random();
            fNum = r.nextInt(9);
            sNum = r.nextInt(9);
            opNum = r.nextInt(ops.length);
            if (opNum == 0)
                answer = fNum + sNum;
            if (opNum == 1)
                answer = fNum - sNum;
            if (opNum == 2)
                answer = fNum / sNum;
            if (opNum == 3)
                answer = fNum * sNum;

            ht.setText(answer);
        }
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ht = findViewById(R.id.hText);



                fN.setText(fNum);
                sN.setText(sNum);
                oP.setText(ops[opNum]);

                int usrAns = Integer.parseInt(valueOf(ans.getText()));

                int rMSG   = r.nextInt(pass.length);

                if(answer == usrAns)
                    out.setText(""+pass[rMSG]);
                    nextQ();
                if(answer != usrAns)
                    out.setText(""+fail[rMSG]);
            }
            });
    }
}

Try to put this code & check whether it works or not? 尝试放入此代码并检查其是否有效?

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
import static java.lang.String.valueOf;

public class MainActivity extends AppCompatActivity {
   TextView fN;
   TextView sN;
   TextView oP;
   TextView out;
   TextView ht;
   EditText ans;
   Button btn;
   int answer;
   int fNum;
   int sNum;
   int opNum;
   Random r;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ht.setVisibility(View.GONE);
    btn = findViewById(R.id.btn);
    fN = findViewById(R.id.fN);
    sN = findViewById(R.id.sN);
    oP = findViewById(R.id.oP);
    out = findViewById(R.id.out);
    ans = findViewById(R.id.Ans);

    final String[] ops = {"+", "-", "÷", "×"};
    final String[] pass = {
            "Very good!",
            "Excellent!",
            "Nice work!",
            "Keep up the good work!"

    };
    final String[] fail = {
            "No. Please try again.",
            "Wrong. Try once more.",
            "Don't give up!",
            "No. Keep trying."
    };

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ht = findViewById(R.id.hText);


            fN.setText(fNum);
            sN.setText(sNum);
            oP.setText(ops[opNum]);

            int usrAns = Integer.parseInt(valueOf(ans.getText()));

            int rMSG = r.nextInt(pass.length);

            if (answer == usrAns)
                out.setText("" + pass[rMSG]);
            nextQ();
            if (answer != usrAns)
                out.setText("" + fail[rMSG]);
        }
    });
}

public void nextQ() {

    //functionalities variables
    r = new Random();
    fNum = r.nextInt(9);
    sNum = r.nextInt(9);
    opNum = r.nextInt(ops.length);
    if (opNum == 0)
        answer = fNum + sNum;
    if (opNum == 1)
        answer = fNum - sNum;
    if (opNum == 2)
        answer = fNum / sNum;
    if (opNum == 3)
        answer = fNum * sNum;
    ht.setText(answer);
}
}

Remove static from function nextQ() . 从函数nextQ()移除static nextQ() And also remove it from onCreate() . 并将其从onCreate()删除。 put it outside on onCreate() . 将其放在onCreate() Function will look like: 函数将如下所示:

onCreate(){ ...  }
public void nextQ() {
....
}

Call it like 像这样称呼它

MainActivity.this.nextQ();

Remove below code form nextQ() and put in onCreate() method: 删除以下代码形式的nextQ()并放入onCreate()方法:

fN = findViewById(R.id.fN);
sN = findViewById(R.id.sN);
oP = findViewById(R.id.oP);
out = findViewById(R.id.out);
ans = findViewById(R.id.Ans);
btn = findViewById(R.id.btn);

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

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