0

我有两个 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() 方法时,一个按钮将重新启动应用程序,而另一个按钮则将其关闭。此外,当我按下后退按钮时,它会关闭应用程序。

谁能解释我如何让以下事情发生:

  1. 当对话框弹出时,我按下后退按钮,它只是关闭对话框?
  2. 当我按下退出按钮时,它会关闭应用程序。
  3. 当我按下停留按钮时,它会关闭对话框。

谢谢

4

1 回答 1

0

你这样做是错误的。使用第二个活动是矫枉过正的。使用 AlertDialog,您可以使用 AlertDialogBu​​ilder 轻松制作。将其设置为具有正面和负面按钮。为在唯一活动上调用完成的肯定按钮设置 OnClickListener。你去 - 1个活动,对话框类本身将负责弹出和​​关闭自己。

于 2013-01-20T07:18:33.953 回答