Android的:如何获得一个模态对话框或类似的模态行为?模态、对话框、如何获得、类似

2023-09-12 04:26:29 作者:灬殇情☆

这些天我工作在Android的仿真模态对话框。我GOOGLE了很多,有很多讨论,但遗憾的是没有太多的选择得到了模式。这里的一些背景, Dialogs,模态对话框和Blockin Dialogs / AlertDialogs:如何与QUOT;块执行"而对话是由(.NET式)

These days I'm working on simulating modal dialog in Android. I've googled a lot, there's much discussions but sadly there's not much options to get it modal. Here's some background, Dialogs, Modal Dialogs and Blockin Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)

有没有直接的方式来获得模态行为,那么我想出了3种可能的解决方案, 1.使用对话为主题的活动,像这样的thread说,但我还是不能让主要活动真正等待对话框活性回报。主要业务转向停止状态,并得到了重新启动即可。 2.建立一个工作线程,并使用线程同步。然而,这对我的应用程序一个巨大的重构工作,现在我有一个主活动,无论是在主UI线程的服务。 3.接管事件循环内处理时,有一个模式对话框了,退出循环,当对话框被关闭。其实这是建立一个像它究竟在Windows中一个真正的模态对话框的方式。我还没有原型这种方式。

There's no straight way to get modal behavior, then I came up with 3 possible solutions, 1. Use a dialog-themed activity, like this thread said, but I still can't make main activity truly wait for dialog-activity return. Main activity turned to stop status and got restarted then. 2. Build one worker thread, and use thread synchronization. However, it's a huge refactoring job for my app, now I have a single main activity and a service both in main UI thread. 3. Take over event handling within a loop when there is a modal dialog up, and quit loop when dialog gets closed. Actually it's the way to build a real modal dialog like what it exactly does in Windows. I still haven't prototyped this way.

我还是想用一个对话为主题的活动,模拟它 通过startActivityForResult 1.启动对话框活动() 2.获得onActivityResult导致() 下面是一些源

I'd still like to simulate it with a dialog-themed activity, 1. start dialog-activity by startActivityForResult() 2. get result from onActivityResult() Here's some source

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MyView v = new MyView(this);
    setContentView(v);
}

private final int RESULT_CODE_ALERT = 1;
private boolean mAlertResult = false;
public boolean startAlertDialog() {
    Intent it = new Intent(this, DialogActivity.class);
    it.putExtra("AlertInfo", "This is an alert");
    startActivityForResult(it, RESULT_CODE_ALERT);

    // I want to wait right here
    return mAlertResult;
}

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case RESULT_CODE_ALERT:
        Bundle ret = data.getExtras();
        mAlertResult = ret.getBoolean("AlertResult");
        break;
    }
}
}

startAlertDialog的主叫方将阻止执行,并期望返回的结果。但startAlertDialog立即返回,当然,和主要活动进入停止状态,而DialogActivity上涨。

The caller of startAlertDialog will block execution and expect returned result. But startAlertDialog returned immediately of course, and main activity went into STOP status while DialogActivity was up.

所以现在的问题是,如何使主要活动真正等待的结果? 谢谢你。

So the question is, how to make main activity really wait for result? Thanks.

推荐答案

我有一个模式对话框,同时使用:

I got a modal Dialog while using:

setCancelable(false);

在DialogFragment(而不是在DialogBu​​ilder)。

on the DialogFragment (not on the DialogBuilder).