繁体   English   中英

android - 回调函数 - 现在显示对话框

[英]android - callback function - Dialog now showing

我有一个 XMPPClient 类,它实现了 Player 接口,用于回调某些函数。 我正在将接口的回调函数从 XMPPManager 调用到函数所在的 XMPPClient 中。

在 XMPPClient 中,我有以下接口回调函数:

 public void request_play(String player) {
    Dialog request_dialog2 = new Dialog(this);
    request_dialog2.setContentView(R.layout.dialog_request2);
    request_dialog2.setTitle(R.string.response_title);
    TextView dialog_message = (TextView)request_dialog2.findViewById(R.id.response_message);
    dialog_message.append(player);
    request_dialog2.setCancelable(true);

    Button button1 = (Button)request_dialog2.findViewById(R.id.response_accept);
    button1.setOnClickListener(this);

    Button button2 = (Button)request_dialog2.findViewById(R.id.response_decline);
    button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        dialog.dismiss();
      }
    });
    request_dialog2.show();
  }

在 XMPPManager 中,我使用以下代码实际调用回调函数:

      PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
      connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {

              if(body.equals("play")) {
                player_avail.request_play("test");
              }
            }
       }

其中连接类型为:XMPPConnection。

我通常使用以下命令调用 XMPPClient:

Intent intent_remote = new Intent(this, XMPPClient.class); 开始活动(intent_remote);

问题是对话框 request_dialog2(在 request_play() 中)没有显示在屏幕上。 如果我创建一个 Activity 而不是 Dialog,则 Activity 运行得很好,但不是 Dialog。

这可能是什么原因?

此外,XMPPManager 没有在单独的线程中运行,因为目前我离开发过程不远,这是我必须添加的东西。

欢迎任何想法。 谢谢你。

只需在对话框的 onResume() 方法中插入一个回调,例如获取对话框数据。 显示对话框后将调用 onResume()。 如果我需要回调来为活动的对话框请求数据,我会在对话框内执行此操作:

@Override
public void onResume() {
    super.onResume();
    if(hasData() && mCallback != null){  // Check if already has data
        mCallback.getDialogData();
    }
}

祝你有美好的一天和美好的编码:D

可能是因为布局? 您可以找到有关创建对话框12 的示例代码的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM