Java 以编程方式关闭由theme.dialog打开的对话框
我有两个xml文件和两个java文件。在第一个xml中,我有几个按钮,其中一个是EXIT。在我在onCreate中编写的java文件中:Java 以编程方式关闭由theme.dialog打开的对话框,java,android,dialog,Java,Android,Dialog,我有两个xml文件和两个java文件。在第一个xml中,我有几个按钮,其中一个是EXIT。在我在onCreate中编写的java文件中: Button exitButton = (Button) this.findViewById(R.id.button_exit); exitButton.setOnClickListener(this); 然后再往下写我写的代码: @Override public void onClick(View v) { switch (v.getId(
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"/>
我的第二个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(),效果很好。但是,当我添加ondestory()方法时,一个按钮将重新启动应用程序,另一个按钮将其关闭。另外,当我按下后退按钮时,它会关闭应用程序
谁能告诉我如何实现以下目标:
谢谢你,你做得不对。使用第二个活动是过分的。使用AlertDialogBuilder可以轻松创建的AlertDialog。将其设置为有一个正极和一个负极按钮。为在唯一活动上调用finish的肯定按钮设置OnClickListener。现在是-1活动,对话框类本身将负责弹出并关闭自己。但是,当用户按下OK按钮时,如何关闭应用程序?我试图添加int-pid=android.os.Process.myPid();android.os.Process.killProcess(pid);但它只是重新启动了整个应用程序而没有关闭它。调用finish()。这将关闭当前的活动。我对android编程是新手。当我按下NO exit按钮时,我在那里设置finish(),它关闭对话框并返回到第一个xml/java,这就是我想要发生的。但它并没有关闭整个应用程序?我尝试了系统。退出(0);它将重新启动应用程序。好的,我发现现在需要做一些事情。我使用moveTaskToBack(true)作为第二个按钮,这样做的技巧(至少在视觉上)。谢谢你的帮助。有谁能告诉我我做错了什么,在这个问题上我得到了-1?为什么这个问题得到了-1?因为你的问题是要求别人为你编码,所以你应该问一些具体的、容易理解的问题,以便成员回答。没有人有时间为你编码。我不是在找人为我编码的人。我所寻找的只是一句描述解决方案的短句。我可以从那里找到我的路。编码是额外的,示例也是如此。但我只是想用words/pseudo提出一个解决方案。否则的话,可能会影响到你。
<?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>