java - Android onTouchListener 不适用于嵌套 switch 语句

标签 java android android-layout

我正在为 Android 开发一款问答游戏。生成一个随机数来随机选择一个问题。当我到达嵌套 switch 语句时,Eclipse 会从这部分给出错误“v 无法解析”: switch(v.getId()) { case R.id.button0: Toast.makeText(BeginGame.this, "success",Toast.LENGTH_SHORT).show();<br/> break; default: Toast.makeText(BeginGame.this,"Fail",Toast.LENGTH_SHORT).show();<br/> break;

有什么建议吗?

`公共(public)类 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");
        }
    }

}

`

最佳答案

你到底想做什么?该方法中没有 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;
        }

    }

关于java - Android onTouchListener 不适用于嵌套 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9272011/

相关文章:

java - 如何比较对象的实例数据

Android列表适配器 - 只给一个值

android - 以编程方式设置后获取奇怪的字体大小

android - 解决 Logcat 警告消息

android - 无黑屏的 Activity 之间的平滑过渡

java - FixedThreadPool 与 CachedThreadPool : the lesser of two evils

带有 HashMap 的 Java XStream

android - 使用 robolectric 测试异步任务

android - 如何使用 inputType ="numberDecimal"向 EditText 添加提示?

java - 替换存储在字符串数组中的换行符