2014-03-14 57 views
-1

在我的活動中,當用戶按下電源按鈕微調顯示成功時,我顯示微調,如果用戶選擇任何選項都可以正常工作,但是當用戶按回它所需的兩次後按以顯示原始活動我該怎麼辦?如何關閉並完成活動只是單個後退?需要兩次警報對話返回到完成活動

public class ActivityHome extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.mainactivity); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if(KeyEvent.KEYCODE_POWER == event.getKeyCode() && isRooted()) {  
     startActivity(new Intent(ActivityHome.this, Spinnermenu.class));   
     return true;  
    } 
    return super.onKeyDown(keyCode, event); 
} 

public class Spinnermenu extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState);  
    final String items[] = {"Power off","Restart"}; 
    AlertDialog.Builder ab=new AlertDialog.Builder(Spinnermenu.this); 
    ab.setTitle("Power Options"); 
    ab.setItems(items, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface d, int choice) { 
     if(choice == 0) {  
        Toast.makeText(Spinnermenu.this,"PowerOff Perform",Toast.LENGTH_SHORT).show();      
     } 
     else if(choice == 1) { 

     } 
    }); 
    ab.show(); 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
     if(keyCode==KeyEvent.KEYCODE_BACK){ 
      this.finish(); 
     } 
     return true; 
    } 
} 
+0

在eclipse中嘗試CTRL + A然後按CTRL + I來修復代碼的縮進 – donfuxx

+1

@SulthanAllaudeen請不要在「提前致謝」。發佈。 –

+0

@Mikeyaworski是的,我會糾正自己。 –

回答

0

你只是把這個代碼在你的程序會做你的需要....

@Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     finish(); 
    } 

所有最好的..

+0

我沒有在調試模式wotk當我按下第二次回來是來這行 如果(的keyCode == KeyEvent.KEYCODE_BACK){ this.finish() – user3416078

+0

telll我如何完成活動時backpress或cancil警報建設者? ? – user3416078

+0

我覺得你需要關閉這兩個活動....是否寫 –

0

剛剛從調用onBackPressed(),

 public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if(keyCode==KeyEvent.KEYCODE_BACK){ 
    // call here 
     onBackPressed(); 
    } 
    return true; 
} 

      @Override 
public void onBackPressed() { 


// just write alertbox option here 

    AlertDialog.Builder ab=new AlertDialog.Builder(Spinnermenu.this); 
ab.setTitle("Power Options"); 
ab.setItems(items, new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface d, int choice) { 
    if(choice == 0) {  
       Toast.makeText(Spinnermenu.this,"PowerOff Perform",Toast.LENGTH_SHORT).show(); 
//add,  
    finish();      
    } 
    else if(choice == 1) { 
//add, 

    finish(); 

    } 
}); 
ab.show(); 


}