android自带菜单键、返回键、搜索键的重写

返回键  

public void onBackPressed() { 

    // TODO Auto-generated method stub  

//方法
    toast.show(); 

}

搜索键

    @Override 

   public boolean onSearchRequested() { 

     // TODO Auto-generated method stub  

//方法

     return super.onSearchRequested(); 

  }

菜单键

@Override 

public boolean onCreateOptionsMenu(Menu menu) { 

  // TODO Auto-generated method stub  

//方法
  return super.onCreateOptionsMenu(menu); 

  } 

@Override 

public boolean onOptionsItemSelected(MenuItem item) { 

   // TODO Auto-generated method stub  

   switch (item.getItemId()) { 

      case 0: 

      finish(); 

      break;

     }

   return super.onOptionsItemSelected(item); 

}



<SPAN style="COLOR: #ff0000">先禁止Home键,再在onKeyDown里处理按键值,点击Home键的时候就把程序关闭</SPAN>



public boolean onKeyDown(int keyCode, KeyEvent event) 

{

    // TODO Auto-generated method stub  

    if(KeyEvent.KEYCODE_HOME==keyCode) 

    android.os.Process.killProcess(android.os.Process.myPid()); 

    return super.onKeyDown(keyCode, event); 

}

@Override 

public void onAttachedToWindow() 

{

      // TODO Auto-generated method stub  

      this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); 

      super.onAttachedToWindow(); 

}       

加权限禁止Home键 

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission> 

你可能感兴趣的:(apple)