Android SDK提供两种View处理机制:
1、Event Listeners(事件侦听器) 2、Event Handlers(事件处理器)一般用于自定义组件重载对应的事件处理函数 View的UI事件侦听器 1、View.OnClickListener的onClick() 2、View.OnFocusChangeListener的onFocusChange() 3、View.OnKeyListener的onKey() 4、View.OnTouchListener的onTouch() 5、View.OnCreateContextMenuListener的onCreatContextMenu() ''关于上下文菜单弹出时的处理 View的UI事件侦听器实现--设置侦听 this.setFocusable(true); this.setFocusableInTouchMode(true); this.setOnClickListener(mClikListener); this.setOnLongClickListerer(mLongClickListener); this.setOnKeyListerer(mKeyListener); this.setOnTouchListerer(mTouchListener); View的UI事件侦听器实现--处理事件 private OnClickListerer mClickListener = new OnClickListener(){ public void onClick(View v){ Log.d(TAG,"mClickListener"); } }; private OnLongClickListerer mLongClickListener = new OnLongClickListener(){ public boolean onLongClick(View v){ Log.d(TAG,"mLongClickListener"); return false; } }; private OnKeyListener mKeyListener = new OnKeyListener (){ public boolean onKey(View v,int keyCode,KeyEvent event){ Log.d(TAG,"mKeyListener"); return false; } }; private OnTouchListener mTouchListener = new OnTouchListener(){ public boolean OnTouch(View v ,MotionEvent event){ Log.d(TAG,"OnTouchListener"); return false; } }; 示例:按button触发OnClick事件显示"mKeyListener": button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(mKeyListener);