启动对话框结果 [英] Start dialog for result

查看:125
本文介绍了启动对话框结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为NumericPicker类,这是在对话框中创建。但它可以在任何活动中使用,所以我不希望使用任何类型的对象或数据集的类。
问题是我需要在活动的方式,当对话框关闭知道这样我就可以救我所需要的值(如 startActivityForResult())。我想我可以在类扩展到活动,并设置主题,以对话,但是这不是想法,类不应该延长活动。

I have created a Class named NumericPicker, which is created in a Dialog. But it can be used in any Activity so I don't want to use any type of Object or Dataset in the Class. The problem is I need to have in the Activity a way to know when the Dialog is closed so I could save the values I need (like startActivityForResult()). I think I could extend the Class to Activity and set the Theme to Dialog, but that's not the idea, the Class should not extend Activity.

任何建议这样做吗?

推荐答案

实施自己的监听器/回调。例如,创建一个onCloseListener接口。您的活动将实现接口,并在对话框实现了 setOnCloseListener 方法。在创建对话框中,指定活动为onCloseListener,当对话框关闭,它调用您的活动实现的接口方法。

Implement your own listener/callback. For example, create an onCloseListener interface. Your activity will implement the interface, and the dialog implements a setOnCloseListener method. When creating the dialog, you assign the activity as the onCloseListener, and when the dialog is closing, it calls the interface method implemented in your activity.

下面是一个监听器接口和实现所谓DrawView自定义视图的内声明的例子:

Here's an example of a listener interface and implementation declared inside of a custom view called DrawView:

public interface OnUndoEventListener {
    public abstract void onUndoEvent();
}

ListenerInfo getListenerInfo() {
    if (mListenerInfo != null) {
        return mListenerInfo;
    }
    mListenerInfo = new ListenerInfo();
    return mListenerInfo;
}

public void setOnUndoEventListener(OnUndoEventListener l) {
    getListenerInfo().mOnUndoEventListener = l;
}

static class ListenerInfo {
    private OnUndoEventListener mOnUndoEventListener;
}

ListenerInfo mListenerInfo;

这是一个活动的实施申报和设置监听器:

This is an activity's implementation declaration and setting the listener:

public class MyActivity extends Activity implements DrawView.OnUndoEventListener {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
        ...
    mDrawView = new DrawView(this, ...);
mDrawView.setOnUndoEventListener(this);

    ...
    };

@Override
public void onUndoEvent() {
    ...
}
...
}

调用回调DrawView:

Invoking the callback in DrawView:

private void someMethod(){

    if (getListenerInfo().mOnUndoEventListener != null)
        getListenerInfo().mOnUndoEventListener.onUndoEvent();

....

};

这篇关于启动对话框结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆