我有两个 xml 文件和两个 java 文件。在第一个 xml 中,我有几个按钮,其中一个是 EXIT。在 java 文件中,我在 onCreate 中写入:
Button exitButton = (Button) this.findViewById(R.id.button_exit);
exitButton.setOnClickListener(this);
然后再往下写我写的代码:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_exit:
Intent switchtoExit = new Intent(StartActivity.this, ExitActivity.class);
startActivityForResult(switchtoExit, MESSAGE_REQUEST);
break;
}
}
第二个 java 文件称为 ExitActivity.java。在清单文件中,我写道:
<activity android:name=".ExitActivity"
android:label="@string/exit_title"
android:theme="@android:style/Theme.Dialog"/>
为了使第二个xml文件像对话框一样弹出。我的第二个java文件是(像对话框一样弹出的那个):
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ExitActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_exit);
Button noExitButton = (Button) this.findViewById(R.id.exit_no_button);
noExitButton.setOnClickListener(this);
Button yesExitButton = (Button) this.findViewById(R.id.exit_yes_button);
yesExitButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.exit_no_button:
Toast.makeText(this, "Good Choice :-D", Toast.LENGTH_SHORT).show();
break;
case R.id.exit_yes_button:
Toast.makeText(this, "So sad... \nnice playing with you...", Toast.LENGTH_SHORT).show();
break;
//break;
}
}
}
我的第二个java文件有:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/red">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/exitTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/exit_body"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/exit_no_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/exit_no_button"
android:layout_weight="1.0"
/>
<Button
android:id="@+id/exit_yes_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/exit_yes_button"
android:layout_weight="1.0"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
我想做的是让一个按钮关闭对话框,另一个按钮停止应用程序。我试过finish(),它工作得很好。但是当我添加 onDestroy() 方法时,一个按钮将重新启动应用程序,而另一个按钮则将其关闭。此外,当我按下后退按钮时,它会关闭应用程序。
谁能解释我如何让以下事情发生:
- 当对话框弹出时,我按下后退按钮,它只是关闭对话框?
- 当我按下退出按钮时,它会关闭应用程序。
- 当我按下停留按钮时,它会关闭对话框。
谢谢