2016-12-26 69 views
-1

我想制作一个button,点击后该应用程序将关闭。单击按钮时如何退出应用程序

我试过所有的方法,但我做不到。

我试图使它像:

public void quit() 
{ 
    System.exit(0); 
} 

,并没有工作。我也试图这样做:

public void quit() 
{ 
    finish(); 
} 

而且它也没有工作。

所以,如果有人能帮助我,请回答这个问题。

非常感谢。

回答

0

试试这个:

@Override 
public void onBackPressed() { 
    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_HOME); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 
} 
0

内部的button的点击监听器,尝试调用之一:

super.onStop();

super.onDestroy();

0

quit()是甲基od,你需要从某处调用它。你在哪里叫它?如果你想调用,当一个按钮被点击这里点击的方式是

在XML中添加一个按钮

<Button 
    android:layout_width="150dp" 
    android:id="@+id/button" 
    android:layout_height="50dp" 
    android:text="exit" 
    android:layout_marginRight="10dp"> 
</Button> 

onCreate

Button buttonOne = (Button) findViewById(R.id.button); 

写回调初始化它被调用时按钮视图被点击

buttonOne.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
      //Do stuff here you can call your method quit or 
      System.exit(0); // this should work if you want to use it 
    } 
}); 

并且有很多方法可读here

相关问题