简体   繁体   English

Android onTouchListener无法通过嵌套switch语句工作

[英]Android onTouchListener does not work from nested switch statement

I'm developing a trivia game for android. 我正在为Android开发琐事游戏。 A random number is generated to select a question at random. 生成一个随机数以随机选择一个问题。 When i get to my nested switch statements eclipse gives an error "v cannot be resolved" from this part: switch(v.getId()) { case R.id.button0: Toast.makeText(BeginGame.this, "success",Toast.LENGTH_SHORT).show();
break; default: Toast.makeText(BeginGame.this,"Fail",Toast.LENGTH_SHORT).show();
break;
当我进入嵌套的switch语句时,eclipse从该部分给出错误“ v无法解决”: switch(v.getId()) { case R.id.button0: Toast.makeText(BeginGame.this, "success",Toast.LENGTH_SHORT).show();
break; default: Toast.makeText(BeginGame.this,"Fail",Toast.LENGTH_SHORT).show();
break;
switch(v.getId()) { case R.id.button0: Toast.makeText(BeginGame.this, "success",Toast.LENGTH_SHORT).show();
break; default: Toast.makeText(BeginGame.this,"Fail",Toast.LENGTH_SHORT).show();
break;

Any suggestions? 有什么建议么?

`public class BeginGame extends Activity { `公共类BeginGame扩展了Activity {

 public OnTouchListener nextListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Log.d("base", "next");
        }
        return false;
        }
 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

        int[] questionArray = {-1,-1,-1,-1,-1,-1,-1};
        Random questionNumberGenerated = new Random();
        int num;
        boolean duplicate = false;


        do{
            num = questionNumberGenerated.nextInt(7);
                for (int i=0; i<questionArray.length; i++){

                    if (num == questionArray[i]){
                        duplicate=true;
                        }
                    }
                }while (duplicate);

        if (duplicate=false){
            for (int j=0; j<questionArray.length; j++)
                questionArray[j]=num;
        }






        switch (num) {
        case 0:             
            Button button0 = (Button) findViewById(R.id.button0);
            button0.setOnTouchListener(nextListener);
            Button button1 = (Button) findViewById(R.id.button1);
            button1.setOnTouchListener(nextListener);
            Button button2 = (Button) findViewById(R.id.button2);
            button2.setOnTouchListener(nextListener);
            Button button3 = (Button) findViewById(R.id.button3);
            button3.setOnTouchListener(nextListener);
            TextView text = (TextView) findViewById(R.id.TextView01);
            text.setText("press alabama:");
            button0.setText("Alabama");
            button1.setText("Mississippi");
            button2.setText("Philadelphia");
            button3.setText("Virginia");    
                switch(v.getId()) {
                case R.id.button0:
                    Toast.makeText(BeginGame.this, "success", Toast.LENGTH_SHORT).show();  
                    break;
                default:
                    Toast.makeText(BeginGame.this, "Fail", Toast.LENGTH_SHORT).show();        
                    break;
            }
        case 1:  
            text.setText("press green");
            button0.setText("zsx");
            button1.setText("zczcz");
            button2.setText("zczc");
            button3.setText("green");
        case 2:  
            text.setText("press blue");
            button0.setText("www");
            button1.setText("aaa");
            button2.setText("dddd");
            button3.setText("blue");
        case 3:  
            text.setText("press red");
            button0.setText("111");
            button1.setText("222");
            button2.setText("3333");
            button3.setText("red");
        }
    }

} }

` `

what you wants to do exactly? 您到底想做什么? there is no definition of v in that method. 在该方法中没有v的定义。 if you wants to perform some task on button touch move the that switch case to onTouch method like following. 如果要在按钮触摸上执行某些任务,请将该开关盒移至onTouch方法,如下所示。

public boolean onTouch(View v, MotionEvent event) {

         switch(v.getId()) {
            case R.id.button0:
                Toast.makeText(BeginGame.this, "success", Toast.LENGTH_SHORT).show();  
                break;
            default:
                Toast.makeText(BeginGame.this, "Fail",  Toast.LENGTH_SHORT).show();        
                break;
        }

    }

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

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