Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在按钮中使用模并设置点击限制?_Java_Android_Button_Modulo - Fatal编程技术网

Java 如何在按钮中使用模并设置点击限制?

Java 如何在按钮中使用模并设置点击限制?,java,android,button,modulo,Java,Android,Button,Modulo,如何设置标签为“帮助”的按钮的点击限制?我使用模运算符(即%)除以分数-对于每5个正确答案,帮助按钮将加上1,并调用.setEnabled(true),如果帮助按钮值等于0,则按钮将被禁用。这是我更改“帮助”按钮状态的代码。使用“帮助”按钮可消除2个错误答案 public class QuestionActivity extends Activity { MediaPlayer mySound; List<Question> quesList; int score = 0; int

如何设置标签为“帮助”的按钮的点击限制?我使用模运算符(即
%
)除以分数-对于每5个正确答案,帮助按钮将加上1,并调用
.setEnabled(true)
,如果帮助按钮值等于0,则按钮将被禁用。这是我更改“帮助”按钮状态的代码。使用“帮助”按钮可消除2个错误答案

public class QuestionActivity extends Activity {

MediaPlayer mySound;
List<Question> quesList;
int score = 0;
int qid = 0;
int i = 0;

//for help 50/50
int help = score % 5;
int rnd2 ,rnd1;

ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;

// Animation
Animation animFadein;


Question currentQ;
TextView txtQuestion, scored;
Button button1, button2, button3, button4,helpbtn;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;
public void help (View view){

    helpbtn=(Button)findViewById(R.id.helpbtn);
    helpbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    help = help - 1;
             v.setEnabled(false);

            /*if (help == 0 ){
                helpbtn.setEnabled(false);
            } else {
                helpbtn.setEnabled(true);
            }
            */
            String AnswerString = currentQ.getANSWER();
            //match DB answer to selected answer, turn it visible if it is correct

            if(button1.getText().equals(AnswerString)){
                button1.setVisibility(View.VISIBLE);
            }
            if(button2.getText().equals(AnswerString)){
                button2.setVisibility(View.VISIBLE);
            }
            if(button3.getText().equals(AnswerString)){
                button3.setVisibility(View.VISIBLE);
            }
            if(button4.getText().equals(AnswerString)){
                button4.setVisibility(View.VISIBLE);
            }

            //random disable 2 incorrect answer
            List<Integer> list = Arrays.asList(1, 2, 3, 4);
            Collections.shuffle(list);
            rnd1 = list.get(0);
            rnd2 = list.get(1);

            if ((rnd1 == 1) || (rnd2 == 1)){
                button1.getText().equals(AnswerString);
                button1.setVisibility(View.INVISIBLE);
            }

            if ((rnd1 == 2) || (rnd2 == 2)){
                button2.getText().equals(AnswerString);
                button2.setVisibility(View.INVISIBLE);
            }

            if ((rnd1 == 3) || (rnd2 == 3)){
                button3.getText().equals(AnswerString);
                button3.setVisibility(View.INVISIBLE);
            }
            if ((rnd1 == 4) || (rnd2 == 4)){
                button4.getText().equals(AnswerString);
                button4.setVisibility(View.INVISIBLE);
            }
        }
    });
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qestion);
    QuizHelper db = new QuizHelper(this);  // my question bank class
    quesList = db.getAllQuestions();  // this will fetch all questions
    currentQ = quesList.get(qid); // the current question
    mySound = MediaPlayer.create(this, R.raw.bensoundcute); // music background
    mySound.start();
    mySound.setLooping(true);

    txtQuestion = (TextView) findViewById(R.id.txtQuestion);
    // load the textQuestion animation
    animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
    // the text view in which the question will be displayed
    // the 4 buttons,
    // the idea is to set the text of 4 buttons with the options from question bank
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);

    // the text view in which score will be displayed
    scored = (TextView) findViewById(R.id.score);
    // method which will set the things up for our game
    setQuestionView(false);

    mProgressBar = (ProgressBar) findViewById(progressbar);
    mProgressBar.setMax(100);
    mProgressBar.setProgress(i);




    mCountDownTimer = new CountDownTimer(30000,300) {
            @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onTick(long millisUntilFinished) {
                Log.v("Log_tag", "Timer Progress " + i  + millisUntilFinished);
                i++;
                mProgressBar.setRotation(180);
                mProgressBar.setProgress(i);

            }
            @Override
            public void onFinish() {
                Toast.makeText(getApplicationContext(), "TIME is UP!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(QuestionActivity.this,
                        ResultActivity.class);
                // passing the int value
                Bundle b = new Bundle();
                b.putInt("score", score); // Your score
                intent.putExtras(b); // Put your score to your next
                startActivity(intent);
                mCountDownTimer.cancel();
                finish();
            }
    };
    txtQuestion.setAnimation(animFadein);
    txtQuestion.startAnimation(animFadein);
    mCountDownTimer.start();
    // button click listeners
    final MediaPlayer buttonSound = MediaPlayer.create(this, R.raw.play);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSound.start();
            if(mCountDownTimer!=null){
                mCountDownTimer.cancel();
            }

            // passing the button text to other method
            // to check whether the answer is correct or not
            // same for all three buttons
            getAnswer(button1.getText().toString());

        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSound.start();
            if(mCountDownTimer!=null){
                mCountDownTimer.cancel();
            }

            getAnswer(button2.getText().toString());

        }
    });
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSound.start();
            if(mCountDownTimer!=null){
                mCountDownTimer.cancel();
            }
            getAnswer(button3.getText().toString());


        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonSound.start();
            if(mCountDownTimer!=null){
                mCountDownTimer.cancel();
            }

            getAnswer(button4.getText().toString());
        }
    });




    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
protected void onPause(){
    super.onPause();
    mySound.release();
    finish();
}
@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);
    builder.setMessage("Do you want to Exit?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //if user pressed "yes", then he is allowed to exit from application
            dialog.dismiss();
            onYesClick();
        }
        private void onYesClick() {
            Intent setIntent = new Intent(Intent.ACTION_MAIN);
            setIntent.addCategory(Intent.CATEGORY_HOME);
            setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(setIntent);
            mCountDownTimer.cancel();
            finish();
            QuestionActivity.this.finish();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //if user select "No", just cancel this dialog and continue with app
            dialog.cancel();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
public void getAnswer(String AnswerString) {

    if (currentQ.getANSWER().equals(AnswerString)) {
        // if conditions matches increase the int (score) by 1
        // and set the text of the score view
       // Intent intent = new Intent(this, QuestionActivity.class);
        //startActivity(intent);
        Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show();
        score++;
        scored.setText("Score:  " + score + " /100");
        txtQuestion.setAnimation(animFadein);
        txtQuestion.startAnimation(animFadein);

    }
    else {
        setQuestionView(false);
        // if unlucky start activity and finish the game
        Toast.makeText(getApplicationContext(), "Sorry! Better luck next time.", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
        // passing the int value
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        mCountDownTimer.cancel();
        finish();
    }


    if(qid < 100) {
        // if questions are not over then do this
        currentQ = quesList.get(qid);
        setQuestionView(true);
        txtQuestion.setAnimation(animFadein);
        txtQuestion.startAnimation(animFadein);

         button1.setVisibility(View.VISIBLE);
         button2.setVisibility(View.VISIBLE);
         button3.setVisibility(View.VISIBLE);
         button4.setVisibility(View.VISIBLE);

    }
    else {
        Toast.makeText(getApplicationContext(),
                "Game Over.",
                Toast.LENGTH_SHORT)
                .show();
        Intent intent = new Intent(QuestionActivity.this,
                ResultActivity.class);
        // passing the int value
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        mCountDownTimer.cancel();
        finish();
    }
}

private boolean setQuestionView(boolean b) {

    // the method which will put all things together
    txtQuestion.setText(currentQ.getQUESTION());
    button1.setText(currentQ.getOPTA());
    button2.setText(currentQ.getOPTB());
    button3.setText(currentQ.getOPTC());
    button4.setText(currentQ.getOPTD());

    qid++;
    return b;
}
公共类提问活动扩展活动{
媒体播放器mySound;
清单问题清单;
智力得分=0;
int-qid=0;
int i=0;
//求救
int help=分数%5;
int rnd2,rnd1;
ProgressBar-mProgressBar;
倒数计时器mCountDownTimer;
//动画
动画动画;
问题Q;
TextView txtQuestion,得分;
按钮1、按钮2、按钮3、按钮4、帮助按钮;
/**
*注意:这是自动生成的,用于实现应用程序索引API。
*看https://g.co/AppIndexing/AndroidStudio 了解更多信息。
*/
私人谷歌客户;
公共void帮助(视图){
helpbtn=(按钮)findViewById(R.id.helpbtn);
helpbtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
帮助=帮助-1;
v、 setEnabled(假);
/*如果(帮助==0){
helpbtn.setEnabled(false);
}否则{
helpbtn.setEnabled(true);
}
*/
String AnswerString=currentQ.getANSWER();
//将数据库答案与所选答案匹配,如果正确,则将其显示
if(button1.getText().equals(AnswerString)){
按钮1.设置可见性(视图.可见);
}
if(button2.getText().equals(AnswerString)){
按钮2.设置可见性(视图.可见);
}
if(button3.getText().equals(AnswerString)){
按钮3.设置可见性(视图.可见);
}
if(button4.getText().equals(AnswerString)){
按钮4.设置可见性(视图.可见);
}
//随机禁用2个错误答案
List=Arrays.asList(1,2,3,4);
集合。洗牌(列表);
rnd1=list.get(0);
rnd2=list.get(1);
如果((rnd1==1)| |(rnd2==1)){
button1.getText().equals(AnswerString);
按钮1.设置可见性(视图.不可见);
}
如果((rnd1==2)| |(rnd2==2)){
button2.getText().equals(AnswerString);
按钮2.设置可见性(视图.不可见);
}
如果((rnd1==3)| |(rnd2==3)){
button3.getText().equals(AnswerString);
按钮3.设置可见性(视图.不可见);
}
如果((rnd1==4)| |(rnd2==4)){
button4.getText().equals(AnswerString);
按钮4.设置可见性(视图.不可见);
}
}
});
}
@RequiresApi(api=Build.VERSION\u code.HONEYCOMB)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qestion);
QuizHelper db=newquizhelper(this);//我的问题库类
quesList=db.getAllQuestions();//这将获取所有问题
currentQ=quesList.get(qid);//当前问题
mySound=MediaPlayer.create(这个,R.raw.bensoundcute);//音乐背景
mySound.start();
设置循环(true);
txtQuestion=(TextView)findViewById(R.id.txtQuestion);
//加载文本问题动画
animFadein=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_-in);
//显示问题的文本视图
//4个按钮,
//我们的想法是用题库中的选项设置4个按钮的文本
button1=(按钮)findViewById(R.id.button1);
button2=(按钮)findViewById(R.id.button2);
button3=(按钮)findViewById(R.id.button3);
button4=(按钮)findViewById(R.id.button4);
//显示分数的文本视图
得分=(TextView)findViewById(R.id.score);
//为我们的游戏做准备的方法
setQuestionView(假);
mProgressBar=(ProgressBar)findViewById(ProgressBar);
最大设置压力(100);
mProgressBar.setProgress(i);
mCountDownTimer=新的倒计时(30000300){
@RequiresApi(api=Build.VERSION\u code.HONEYCOMB)
@凌驾
公共void onTick(长毫秒未完成){
Log.v(“日志标签”,“计时器进度”+i+millisuntFinished);
i++;
mProgressBar.设置旋转(180);
mProgressBar.setProgress(i);
}
@凌驾
公共无效onFinish(){
Toast.makeText(getApplicationContext(),“时间到了!”,Toast.LENGTH\u SHORT.show();
意向意向=新意向(QuestionActivity.this,
结果类);
//传递int值
Bundle b=新Bundle();
b、 putInt(“分数”,分数);//你的分数
意图。额外加分(b);//把你的分数记到下一个分数上
星触觉(意向);
mCountDownTimer.cancel();
完成();
}
};
设置动画(animFadein);
txtQuestion.startAnimation(animFadein);
mCountDownTimer.start();
//按钮单击侦听器
最终MediaPlayer按钮声音=MediaPlayer.create(这个,R.raw.play);
button1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
按钮sound.start();
如果(mCountDownTimer!=null){
mCountDownTimer.cancel();
}
//将按钮文本传递给其他方法
//检查答案是否正确
 if (currentQ.getANSWER().equals(AnswerString)) {
    // if conditions matches increase the int (score) by 1
    // and set the text of the score view
   // Intent intent = new Intent(this, QuestionActivity.class);
    //startActivity(intent);
    Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show();
    score++;
    scored.setText("Score:  " + score + " /100");
    txtQuestion.setAnimation(animFadein);
    txtQuestion.startAnimation(animFadein);


        if (score%5 == 1 ){
            helpbtn.setEnabled(false);
        } else {
            helpbtn.setEnabled(true);
        }


}