Button.setOnClickListener(mGoBack);
public OnClickListener mGoBack = new OnClickListener() {

    public void onClick(View v) {
        finish();
    }
};





要返回上一个activity,只需结束当前的activity



有两Activity~A和BA运行启动BActivity保留A情形B结束时候传回参数A~~~


我用startActivity发现创建了新Activity~~~


能保留A现场啊


A启动B时样写



Intent intent=new Intent(MainActivity.this,SecMainActivity.class);
 startActivityForResult(intent, 1);

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub ...




在android中重写父类的onkeydown方法,就可以更方便的操作了。


Android代码 收藏代码


@Override 

public boolean onKeyDown(int keyCode, KeyEvent event) { 

// TODO Auto-generated method stub 

if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){ 

 //需要处理 

} 

 return false; 

} 

 长按事件 

Java代码 收藏代码 

@Override 

public boolean onKeyLongPress(int keyCode, KeyEvent event) { 

 if (keyCode == KeyEvent.KEYCODE_CALL) { 

 // a long press of the call key. 

 // do our work, returning true to consume it. by 

 // returning true, the framework knows an action has 

 // been performed on the long press, so will set the 

 // canceled flag for the following up event. 

 return true; 

 } 

 return super.onKeyLongPress(keyCode, event); 

} 



Java代码 收藏代码 

@Override 

public boolean onKeyUp(int keyCode, KeyEvent event) { 

 if (keyCode == KeyEvent.KEYCODE_CALL && event.isTracking() 

 && !event.isCanceled()) { 

 // if the call key is being released, AND we are tracking 

 // it from an initial key down, AND it is not canceled, 

 // then handle it. 

 return true; 

 } 

 return super.onKeyUp(keyCode, event); 

} 


@Override 

public boolean onKeyDown(int keyCode, KeyEvent event) { 

 if (keyCode == KeyEvent.KEYCODE_0) { 

 // this tells the framework to start tracking for 

 // a long press and eventual key up. it will only 

 // do so if this is the first down (not a repeat). 

 event.startTracking(); 

 return true; 

 } 

 return super.onKeyDown(keyCode, event); 

} 


@Override 

public boolean dispatchKeyEvent(KeyEvent event) { 

 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { 

 if (event.getAction() == KeyEvent.ACTION_DOWN 

 && event.getRepeatCount() == 0) { 


 // Tell the framework to start tracking this event. 

 getKeyDispatcherState().startTracking(event, this); 

 return true; 


 } else if (event.getAction() == KeyEvent.ACTION_UP) { 

 getKeyDispatcherState().handleUpEvent(event); 

 if (event.isTracking() && !event.isCanceled()) { 


 // DO BACK ACTION HERE 

 return true; 


 } 

 } 

 return super.dispatchKeyEvent(event); 

 } else { 

 return super.dispatchKeyEvent(event); 

 } 

}