gpt4 book ai didi

java - 试图在线程中按下PopupWindow的按钮

转载 作者:行者123 更新时间:2023-12-03 13:10:18 25 4
gpt4 key购买 nike

我有一个尝试连接到服务器的客户端。如果连接失败,则在5秒钟后超时。我想打开一个没有连接符号和“重试连接”按钮的弹出窗口。弹出窗口是在线程中创建的,因此,如果连接失败,线程将结束。因此,我在主要 Activity 中加入了click方法。为什么不起作用?

连接失败后线程运行的代码:

((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = (LayoutInflater)
((Activity) context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PopupWindow noConnection = new PopupWindow(
inflater.inflate(R.layout.popup_no_connection, null, false),
800,
800,
true);
noConnection.setAnimationStyle(R.style.AnimationFade);
noConnection.showAtLocation(((Activity) context).findViewById(R.id.entrance_layout), Gravity.CENTER, 0, 0);
globalClass.getErrorHandler().setNoConnectionWithServer(noConnection);
}
});

主要 Activity 中的代码:
((GlobalClass) getApplicationContext()).initiateClass();
globalClass = ((GlobalClass)getApplicationContext());

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupWindowView = inflater.inflate(R.layout.popup_no_connection, null, false);
Button refreshConnection = (Button) popupWindowView.findViewById(R.id.btn_reconnect);
refreshConnection.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (globalClass.getErrorHandler().getNoConnectionWithServer() != null) {
Communication communication = new Communication(globalClass, entrance.this);
globalClass.setCommunication(communication);
Thread t = new Thread(communication);
t.start();
globalClass.getErrorHandler().getNoConnectionWithServer().dismiss();
}
}
});

最佳答案

当然,不会调用onClick回调。为什么?因为:

// you find button in a view 
View popupWindowView = inflater.inflate(R.layout.popup_no_connection, null, false); // this return new view
Button refreshConnection = (Button) popupWindowView.findViewById(R.id.btn_reconnect);

// but you create pop up with another view not from above (which contain listener)
final PopupWindow noConnection = new PopupWindow(
inflater.inflate(R.layout.popup_no_connection, null, false),
800,
800,
true);

那么如何解决呢?有两种方法:
1.在 inflate编码后保存您的弹出 View ,并在 runnable中创建弹出 View 时重新使用。
2.创建 Button后在 setOnclickListener中找到您的 Runnablepop-up view

关于java - 试图在线程中按下PopupWindow的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com