Android 按钮响应问题

发布于 2024-11-06 05:58:39 字数 173 浏览 3 评论 0原文

大家好 因为我正在使用一个按钮,并且单击该按钮后它会连接到一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然#的苍凉 2024-11-13 05:58:39

将以下代码放入您的类中:

 // Need handler for callbacks to UI Threads
    // For background operations
    final Handler mHandler = new Handler();

    // Create Runnable for posting results
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            // Do your task which needs to get done after webservice call is complete.
        }
    };

为了调用 Web 服务,请在按钮事件中使用以下代码:

new Thread() {
            public void run() {             
                // Place the webservice call here.   

                mHandler.post(mUpdateResults);
            }
            }.start();

实际上您正在寻找的是多线程,所有 Web 服务调用和网络活动都应该在单独的线程中进行。
在线程 start() 调用之后,您可以做任何您想做的事情,并且会立即完成,没有任何延迟(在您的情况下显示按下按钮)。

Put the following code in your class:

 // Need handler for callbacks to UI Threads
    // For background operations
    final Handler mHandler = new Handler();

    // Create Runnable for posting results
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            // Do your task which needs to get done after webservice call is complete.
        }
    };

And for calling the webservice use the following code in button event:

new Thread() {
            public void run() {             
                // Place the webservice call here.   

                mHandler.post(mUpdateResults);
            }
            }.start();

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).

微暖i 2024-11-13 05:58:39

您必须使用处理程序来执行此后台操作,已在操作系统上询问,请遵循此链接
进度对话框未在 Android 中显示?

You have to use Handler for this background operation already ask on OS follow this link
progress dialog not showing in android?

可是我不能没有你 2024-11-13 05:58:39

您应该编写一个类(例如 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.

云仙小弟 2024-11-13 05:58:39

在 onClickListener 中创建一个新线程,在后台完成繁重的工作。这样 UI 线程将能够更新按钮的状态:

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        new Thread(new Runnable() {

        @Override
        public void run() {
            // Code that connects to web service goes here...
        }
    }).start();
});

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:

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        new Thread(new Runnable() {

        @Override
        public void run() {
            // Code that connects to web service goes here...
        }
    }).start();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文