Android onTouchListener无法使用嵌套的switch语句

时间:2012-02-14 05:09:28

标签: java android android-layout

我正在为Android开发一个琐事游戏。生成随机数以随机选择问题。当我到达我的嵌套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;

有什么建议吗?

`public class BeginGame extends 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");
        }
    }

}

`

1 个答案:

答案 0 :(得分:1)

你想要做什么?该方法中没有v的定义。 如果你想在按键触摸上执行某项任务,请将该开关盒移动到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;
        }

    }