首页 > 解决方案 > 如何在android studio中使用isChecked

问题描述

如此简单的问题,我无法弄清楚我在代码末尾不断遇到的错误

这是一个像儿童游戏一样运行的简单应用程序,当我在每个广播组上使用 onclicklistener 来检查答案时,其中显示了图片和从代码中选择的 3 个选项工作正常,但现在我想完成检查在我制作的“检查”按钮上,并在 android studio 中使用“isPressed”方法,现在我的代码出现错误

这是代码

package com.example.hamdanali.imageview;

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

import java.util.LinkedHashSet;
import java.util.Random;

public class imagegame extends AppCompatActivity {

ImageView pic ;
Button    btn1;
Button    btn2;
Button    btn3;
Button    check;
EditText score;

int array[]={
        R.drawable.one  ,
        R.drawable.two  ,
        R.drawable.three,
        R.drawable.four ,
        R.drawable.five ,
        R.drawable.six  , };

String picname[]={

        "One"  ,
        "Two"  ,
        "Three",
        "Four" ,
        "Five" ,
        "Six"  };
int correctanswer=69;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_imagegame);

    pic =   findViewById(R.id.Pic) ;
    btn1=   findViewById(R.id.Btn1);
    btn2=   findViewById(R.id.Btn2);
    btn3=   findViewById(R.id.Btn3);
    check=  findViewById(R.id.Check);
    score=  findViewById(R.id.SCOREE);

    int randomepicutre = (int) (Math.random()*array.length);
    pic.setImageResource(array[randomepicutre]);

   int randomebuttonposition = (int) (Math.random()*3);
   switch (randomebuttonposition) {
       case 0:
           btn1.setText(picname[randomepicutre]);
           btn2.setText(picname[(randomepicutre + 1) % 3]);
           btn3.setText(picname[(randomepicutre + 2) % 3]);
           correctanswer = 1;
           break;
       case 1:
           btn2.setText(picname[randomepicutre]);
           btn1.setText(picname[(randomepicutre + 1) % 3]);
           btn3.setText(picname[(randomepicutre + 2) % 3]);
           correctanswer = 2;
           break;
       case 2:
           btn3.setText(picname[randomepicutre]);
           btn2.setText(picname[(randomepicutre + 1) % 3]);
           btn1.setText(picname[(randomepicutre + 2) % 3]);
           correctanswer = 3;
           break;
   }

   check.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           int randomepicutre = (int) (Math.random() * array.length);
           pic.setImageResource(array[randomepicutre]);
           score.setText(" ");

           if (btn1.isPressed()) { //instead of isPressed try to use isChecked

               if (correctanswer == 1)
                   score.setText("correct answer");
               else {
                   score.setText("try again");
               }
           }
           if (btn2.isPressed()) {//instead of isPressed try to use isChecked
               if (correctanswer == 2)
                   score.setText("correct answer");
               else {
                   score.setText("try again");
               }
           }
           if (btn3.isPressed()) {
               if (correctanswer == 3)
                   score.setText("correct answer");
               else {
                   score.setText("try again");
               }
           }

           int randomebuttonposition = (int) (Math.random() * 3);
           switch (randomebuttonposition) {
               case 0:
                   btn1.setText(picname[randomepicutre]);
                   btn2.setText(picname[(randomepicutre + 1) % 3]);
                   btn3.setText(picname[(randomepicutre + 2) % 3]);
                   correctanswer = 1;
                   break;
               case 1:
                   btn2.setText(picname[randomepicutre]);
                   btn1.setText(picname[(randomepicutre + 1) % 3]);
                   btn3.setText(picname[(randomepicutre + 2) % 3]);
                   correctanswer = 2;
                   break;
               case 2:
                   btn3.setText(picname[randomepicutre]);
                   btn2.setText(picname[(randomepicutre + 1) % 3]);
                   btn1.setText(picname[(randomepicutre + 2) % 3]);
                   correctanswer = 3;
                   break;

           }
       }



     // under this line is where I keep getting the error

     }})})}

很抱歉,如果我的代码很乱,我刚开始学习 android studio

标签: javaandroidandroid-studio

解决方案


点击代码 -> 重新格式化代码,你会看到有什么问题

应该如何:

            // under this line is where I keep getting the error
        });
    }
}

原因:
你有太多不需要的牙套。在带有错误注释的行之后,您的 onClickListener 结束了。你应该关闭它。当它创建匿名类 ( check.setOnClickListener(new View.OnClickListener() {) 时,您应该关闭匿名类和方法调用,因此});. 如果我们省略 listener 的内部部分,它将看起来像这样。

check.setOnClickListener(new View.OnClickListener() { ... });

然后你应该关闭onCreate(),然后是活动类主体。

            // under this line is where I keep getting the error
        }); // check.setOnClickListener(new View.OnClickListener() {
    }       // onCreate() {
}           // public class imagegame extends AppCompatActivity {

推荐阅读