提问者:小点点

我如何退出我的应用程序在菜单项点击?


我如何退出我的应用程序在导航栏菜单项点击。 我也使用导航栏来执行其他任务,但我对直接点击项目退出应用程序感到有点困惑

        switch (menuItem.getItemId()) {
            case R.id.nav_home:
                break;

            case R.id.nav_delete:
                TODOViewModel.deleteAllTasks();
                Toast.makeText(this, "All TODOs Deleted", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.nav_logout:
                Intent intent = new Intent(MainActivity.this, ActivityLogin.class);
                startActivity(intent);
                finish();
                break;

            case R.id.nav_exit:

            default:
        }

共2个答案

匿名用户

使用finish()或system.exit(0)。

匿名用户

这是使用的XML文件

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent">
   <TextView android:layout_width="fill_parent"
      android:layout_height="wrap_content" android:id="@+id/txt1" android:text="txt1" />
   <TextView android:layout_width="fill_parent"
     android:layout_height="wrap_content" android:id="@+id/txt2" android:text="txt2"/>
   <Button android:layout_width="fill_parent"
      android:layout_height="wrap_content" android:id="@+id/btn1"
       android:text="Close App" />
  </LinearLayout>


Java文件

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class testprj extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           // TODO Auto-generated method stub
           finish();
        System.exit(0);
       }
   });
}

}