当前位置: 代码迷 >> Android >> android.view.View中的事件处理,没有反应,该如何处理
  详细解决方案

android.view.View中的事件处理,没有反应,该如何处理

热度:232   发布时间:2016-05-01 13:29:10.0
android.view.View中的事件处理,没有反应
运行程序后,按键触屏全部没有反应
Java code
public class ViewEvent extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(new GameView(this));    }}

Java code
public class GameView extends View implements Runnable {    Context mContext = null;    public GameView(Context context) {        super(context);        mContext = context;    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event)    {        switch (keyCode)        {        case KeyEvent.KEYCODE_DPAD_UP:            DisplayToast("按下:上键");            break;        }        return super.onKeyDown(keyCode, event);    }        @Override    public boolean onKeyUp(int keyCode, KeyEvent event)    {        switch(keyCode)        {        case KeyEvent.KEYCODE_DPAD_CENTER:            DisplayToast("弹起:中建");            break;        }        return super.onKeyUp(keyCode, event);    }        @Override    public boolean onTouchEvent(MotionEvent event)    {        int iAction = event.getAction();        if (iAction == MotionEvent.ACTION_CANCEL ||             iAction == MotionEvent.ACTION_DOWN ||             iAction == MotionEvent.ACTION_MOVE)        {            return false;        }                //获得触笔的位置        int x = (int)event.getX();        int y = (int)event.getY();                DisplayToast("触笔点击位置:(" + x + ", " + y + ")");        Log.e("GameView", "in onTouchEvent");        return super.onTouchEvent(event);    }        @Override    public void run() {        while (!Thread.currentThread().isInterrupted())        {            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                Thread.currentThread().interrupt();            }            this.postInvalidate();        }    }        public void DisplayToast(String str)    {        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();    }}


------解决方案--------------------
修改的地方1:
public GameView(Context context) {
super(context);
setClickable(true);
setFocusable(true);

mContext = context;
}
修改地方2:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_UP:
DisplayToast("按下:上键");
break;
}
return false;
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
DisplayToast("弹起:中建");
break;
}
return false;
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
int iAction = event.getAction();
if (iAction == MotionEvent.ACTION_CANCEL || 
iAction == MotionEvent.ACTION_DOWN || 
iAction == MotionEvent.ACTION_MOVE)
{
return true;
}

//获得触笔的位置
int x = (int)event.getX();
int y = (int)event.getY();

DisplayToast("触笔点击位置:(" + x + ", " + y + ")");
Log.e("GameView", "in onTouchEvent");
return true;
}

你都是return的super.(**);
  相关解决方案