开发者

Problems setting visibility in Android application

开发者 https://www.devze.com 2023-02-18 05:45 出处:网络
I\'m writing an Android application which requires the user to login. I have a \"username\" and \"password\" EditText fields and also a \"Submit\" button.

I'm writing an Android application which requires the user to login. I have a "username" and "password" EditText fields and also a "Submit" button.

When testing in the emulator (and on a phone), the app appears to 'lock up' when the user hits "Submit". It hasn't actually crashed or anything like that, it's just locked whilst the connection to the server is made (and then it advances the user to the next view if their login is correct).

This isn't very cosmetically pleasing as some users may think the app has locked up if their connection is slow and the login doesn't process for a few seconds..

To avoid this I want to have a ProgressBar which will appear just to let the user know something is happening and the app hasn't locked up!

Here's my code:

private OnClickListener listenerOne = new OnClickListener() {
    public void onClick(View v) {
        Button submitButton = (Button)findViewById(R.id.submit);
        submitButton.setVisibility(8); //Make submit button invisible

        ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressbar);
        progressBar.setVisibility(0); //Make progress bar visible. It's in the same position as the submit button

        login(); // do开发者_StackOverflow社区 the login server stuff
                // the problem is that thenew visibility doesn't happen until the login() is called...
    }
};

I've ordered the code so that it makes the submit invisible, then makes the progress bar visible, then does the login (so the progress bar will be there twirling around whilst the app connects to the login server). However, it's not working out how I intended - it's seemingly skipping over the setVisibility code and just calling the login method. It must be setting the visibility of the submit and progress bar items but it doesn't happen before the login method does its stuff as it just locks up like usual and the items never actually get hidden/shown until the login method has completed!!

Any help would be much appreciated. Sorry for the essay!


You need to hand control back to the UI thread after changing the ProgressBar visibility and do your login work on another thread. The best way to do this is via AsyncTask.

This answer has a code sample for showing a ProgressDialog while doing a task in the background. You can modify it to call setVisibility on your ProgressBar instead.


The login part is still taking over the UI thread despite being "after" your UI elements.

You should try running it in a separate thread or AsyncTask. Try this for starters (replace your login() call with it), then make it prettier if it works:

    new Thread(new Runnable() {
        public void run() {
            login();
        }
    }).start();

Any particular reason you are using the actual ints as opposed to the flags available for setting visibility? Seems like it would be easier to read like this:

progressBar.setVisibility(ProgressBar.VISIBLE);
0

精彩评论

暂无评论...
验证码 换一张
取 消