单击对话框中的确定按钮后关闭应用程序

时间:2015-04-13 09:07:21

标签: android dialog

我有自定义对话框,我希望在用户点击确定按钮时关闭应用程序。我该怎么做?

public class PromptDialog extends Dialog {
    TextView removeMsg;
    public PromptDialog(Context context){
        super(context);
        setContentView(R.layout.dlg_prompt);
        removeMsg = (TextView)findViewById(R.id.confirmText);
        Button okBut = (Button)findViewById(R.id.confirmBut);
        okBut.setOnClickListener(okButClick);
    }
    View.OnClickListener okButClick = new View.OnClickListener() {
        public void onClick(View v) {
        dismiss();
        }
    };
}

2 个答案:

答案 0 :(得分:1)

而不是关闭对话框,您可以获取调用此对话框的活动,并完成它。

View.OnClickListener okButClick = new View.OnClickListener() {
        public void onClick(View v) {
        getActivity().finish();
        }
    };

答案 1 :(得分:0)

假设您有这样的活动:

public class MainActivity extends ActionBarActivity implements PromptDialog.CommunicationWithActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


PromptDialog promptDialog = new PromptDialog(this/*your context*/, this/*Your communication interface*/);
        promptDialog.show(); // call your dialog and pass interface to it
        }

    public void finishActivity() {
        this.finish();
    }
}

在Dialog类中:

public class PromptDialog extends Dialog {
    TextView removeMsg;

    private CommunicationWithActivity mCallBack;

    public interface CommunicationWithActivity {
        public void finishActivity();
    }

    public PromptDialog(Context context, CommunicationWithActivity mCallBack){
        super(context);
        this.mCallBack = mCallBack;
        setContentView(R.layout.dlg_prompt);
        removeMsg = (TextView)findViewById(R.id.confirmText);
        Button okBut = (Button)findViewById(R.id.confirmBut);
        okBut.setOnClickListener(okButClick);
    }
    View.OnClickListener okButClick = new View.OnClickListener() {
        public void onClick(View v) {
            mCallBack.finishActivity();
            dismiss();
        }
    };
}