单击按钮时如何退出应用程序

时间:2016-12-26 15:56:36

标签: android button

我想创建一个button,点击后应用程序将关闭。

我尝试了所有方法,但我无法做到。

我试着这样做:

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

它没有用。我也试着这样做:

public void quit()
{
    finish();
}

它也没有用。

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

非常感谢你。

3 个答案:

答案 0 :(得分: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);
 }

答案 1 :(得分:0)

button的点击监听器内,尝试调用以下任意一个:

super.onStop();

super.onDestroy();

答案 2 :(得分:0)

quit()是一种方法,你需要从某个地方调用它。你在哪里叫它?如果你想在点击按钮时调用它,那就是

在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

的方法有很多种