使用退出按钮关闭应用程序 [英] Closing Application with Exit button
本文介绍了使用退出按钮关闭应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 android 的初学者,我正在练习一个有 2 个标签和 1 个退出按钮的项目.但是当我在 android 手机上运行这个项目时,退出按钮不起作用,它根本不会退出.
I'm a beginner in android, I'm practicing a Project that have a 2 labels and 1 exit button. But when I run this project in android phone the exit button is not working, it won't exit at all.
如何使退出按钮起作用?
How can I make exit button work?
推荐答案
下面用到的main.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>
<小时>
和 text.java 文件在下面
and text.java file is below
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);
}
});
}
}
这篇关于使用退出按钮关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文