0

At the push of a button the application tries to send a data to a server. If the connection is active, the data is immediately sent, but if the link is down, the system attempts to send for x seconds. In fact, in that period of time, the interface is (intentionally) blocked (the button is illuminated to highlight the current operation). The problem is that if the user starts to press other buttons, the events are heard and when the connection becomes active, it performs all the operations related to those events.
How can I prevent it?
How to prevent that How to prevent each event related to other buttons to be processed? Any suggestions would be very appreciated.

EDIT I've already tried to check a flag when the button is pressed, unfortunately when the flag is re-setted, all events are catched and executed:

public void onClick(View v) {                   
    switch(v.getId())
    {

    case R.id.button1:  //when sendData() is trying to send the message, I don't want to that this code is executed
       if(flagConf  == 1)  
       {
                ...do something...
       }
       break;
    case R.id.buttonConfirm:

        if(myFlag  == 1)    //to avoid multiple touch of this button
        {
            ...do something...
            myFlag = 0;
            sendData(); 
            if(dataSent)     
             ...do something...
            else
            ...do something...
            myFlag = 1;  //I set this flag to 1 because, if message is not sent, the user have to re-pressed the buttonConfirm and try again
        }     
     break;
    }
 }

And on other button listeners:

Thanks

4

2 回答 2

1

您可以简单地使用boolean = true;在数据处理时设置为类似的标志。然后签入Button每个onClick

public void onClick(View v)
{
     if (!sending)
     {
         // do stuff
     }
}

如果sending是,true那么Button将什么也不做。请记住在处理完数据或其他任何内容后将其设置为flag后退。false您也可以使用 buttonName.setEnabled(false); then back to完成后为真`。

旁注:如果这个操作需要很长时间(可能超过一两秒),我会劝阻不要让他们做任何事情,因为人们已经变得非常不耐烦。没有人愿意坐着看一个旋转的圆圈几秒钟。但是,如果您选择这样做,那么您可能需要显示一些消息,以便他们知道为什么Buttons不起作用

于 2013-06-05T20:11:28.880 回答
1

我有一个类似的问题。有一个很好的方法来解决这个问题。

我与服务器的连接是通过扩展的类完成的AsyncTask。这样,每次我按下按钮时,我都会检查任务是否正在运行。如果不是,它将生成一个新任务并执行该任务。

public void onClick(View v) {
    if (updateThread == null){

        updateThread = new UpdateThreadClass();
        updateThread.execute();
    }
}

然后像这样创建 AsyncTask:

   public class UpdateThreadsClass extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... params) {

        // Your code
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        // Execute after completing the connection
    }
}

将变量声明updateThread为全局变量,然后就可以了。

如果需要,您可以通过在任务完成之前和之后XML更改可见性(.setVisibility(View.VISIBLE).setVisibility(View.Gone)内部onClick)来激活进度条,也可以对按钮执行相同操作。

编辑

您可以将进度条放在XML这样的位置:

<ProgressBar
            android:id="@+id/loading_screen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:layout_centerVertical="true"
            android:gravity="center_vertical|center_horizontal"
            android:visibility="gone"/>

然后单击按钮后,只需loadingScreen = (ProgressBar) findViewById(R.id.loading_screen)点赞并设置可见性可见。

于 2013-06-05T20:16:30.173 回答