lezilisy 2021-03-18 20:21 采纳率: 42.9%
浏览 57
已结题

主活动如何与AlertDialog进行交互?

我想把主活动接收到的数据实时地显示在alertdialog上,点击alertdialog中自定义的按钮能够调用主活动中的发送数据的方法,但是实际调试发现按钮不响应,数据也不显示

alertDialog=new AlertDialog.Builder(SecondActivity.this)
                        .setView(R.layout.dialog_window)
                        .create();
                
                LayoutInflater inflater = LayoutInflater.from(SecondActivity.this);
                View dialogView=inflater.inflate(R.layout.dialog_window, null);

                Button sendMessage = (Button)dialogView.findViewById(R.id.sendMessage);
                final EditText editText=(EditText) dialogView.findViewById(R.id.editText);
                responseData=(TextView) dialogView.findViewById(R.id.response_data);

                sendMessage.setOnClickListener(view -> {
                    if(alertDialog!=null && alertDialog.isShowing()) {
                        Log.e("alertdialog", "onCreate: ");
                        String message = editText.getText().toString();
                        if (message != null) {
                            byte[] byteMessage = message.getBytes();
                            writeBytes(byteMessage);
                        }
                    }
                });
                alertDialog.show();
  • 写回答

3条回答 默认 最新

  • nades 2021-03-19 09:37
    关注

    可以使用

    interface

    把dialog按钮的事件分发给activity.

     

    也可以使用service

    也可以使用eventbus 等通信工具。

    以上三种方法都可以做到。

    评论

报告相同问题?