Android 按钮响应问题
大家好 因为我正在使用一个按钮,并且单击该按钮后它会连接到一个 Web 服务。 但问题是,当我按下按钮时,它没有显示它已被单击并连接到互联网和网络服务。连接后,它向我显示已单击的响应。总之按钮的反应很慢。如果该按钮在其监听器中有一些互联网连接。 我知道这与 UI 线程有关。但请朋友们指导我完成这个。
非常感谢,
hi all
since i am using a button and on the click of that button it connects to a Web Service.
But the problem is that when i press the button it does not showed me that it has been clicked and goes to connect to the internet and web service. after connecting it shows me the response that it has been clicked. in short the response of button is very slow. if that buton has some INternet connectvity in its Listener.
i know it has something to do with UI thread. but please friends guide me through this.
Thanks a bunch,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将以下代码放入您的类中:
为了调用 Web 服务,请在按钮事件中使用以下代码:
实际上您正在寻找的是多线程,所有 Web 服务调用和网络活动都应该在单独的线程中进行。
在线程 start() 调用之后,您可以做任何您想做的事情,并且会立即完成,没有任何延迟(在您的情况下显示按下按钮)。
Put the following code in your class:
And for calling the webservice use the following code in button event:
Actually what are you looking for is multithreading, all the webservice calls and network activities should go in separate thread.
After the thread start() call you can do what ever you want and would be done instantly without any delay (in your case showing that button pressed).
您必须使用处理程序来执行此后台操作,已在操作系统上询问,请遵循此链接
进度对话框未在 Android 中显示?
You have to use Handler for this background operation already ask on OS follow this link
progress dialog not showing in android?
您应该编写一个类(例如 MyWebService)并从 AsyncTask 扩展它。在其重写的 doInBackground() 方法中执行连接操作,并在其 onPostExecute() 方法中更新任何 UI 更改。
You should write a class say MyWebService and extend it from AsyncTask. Perform the connect operation in its overridden doInBackground() method and update any UI changes in its onPostExecute() method.
在 onClickListener 中创建一个新线程,在后台完成繁重的工作。这样 UI 线程将能够更新按钮的状态:
Create a new Thread in the onClickListener that does the heavy work in the background. That way the UI thread will be able to update the state of the button: