【问题标题】:Random number onTouchEvent in AndroidAndroid中的随机数onTouchEvent
【发布时间】:2013-08-05 00:03:38
【问题描述】:

我打算在android平台上用touchevent生成随机数

public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        System.out.println(" touched " );
        //int test = rand.nextInt(5);

        System.out.println(" touched " + rand.nextInt(5));
        return super.onTouchEvent(event);
    }

但是,当我触摸屏幕时,它会显示以下错误“应用程序已意外停止”。不知道代码有什么问题。任何帮助将不胜感激。

【问题讨论】:

  • 尝试改变 return super.onTouchEvent(event);返回错误;如果不行就写。
  • 感谢 divoom12。我改变了,但是,我得到了同样的错误

标签: android random touch-event


【解决方案1】:

试试这个。它与您的不完全一样,但有效:

button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN )
    {
            Random r = new Random();
            int i1=r.nextInt(80-65) + 65; // from 65, to 80
            Toast.makeText(getApplicationContext(), String.valueOf(i1), Toast.LENGTH_SHORT).show();
            return true;
    }
    return false; 

}));

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 2014-04-29
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多