如何结束我们的android程序

MainActivity.this.finish();//操作结束

如何结束我们的android程序

点击退出,退出程序,通过图片按钮实现

.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageButton 
        android:id="@+id/imgbut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/exit"/>

</LinearLayout>

.java

package com.example.dialogdemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends Activity {
private ImageButton imgButton=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imgButton=(ImageButton) super.findViewById(R.id.imgbut);
//为我们的图片添加事件
this.imgButton.setOnClickListener(new OnClickListenerImp());
}
public class OnClickListenerImp implements OnClickListener{

public void onClick(View arg0) {
Dialog dialog=new AlertDialog.Builder(MainActivity.this)
.setTitle("程序退出?")
.setMessage("您要退出吗")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//确定的话就表示退出,此时我们结束我们程序
//使用我们Activity提供的finish方法
MainActivity.this.finish();//操作结束
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create();
dialog.show();
}
}
}

你可能感兴趣的:(如何结束我们的android程序)