Android 应用程序中设置可见性时出现问题
我正在编写一个需要用户登录的 Android 应用程序。我有一个“用户名”和“密码”EditText 字段以及一个“提交”按钮。
在模拟器(和手机)中测试时,当用户点击“提交”时,应用程序似乎“锁定”。它实际上并没有崩溃或类似的情况,它只是在与服务器建立连接时被锁定(然后如果用户的登录正确,它会将用户前进到下一个视图)。
这在外观上不太令人愉快,因为一些用户可能会认为,如果他们的连接速度很慢并且登录在几秒钟内没有处理,那么应用程序已经锁定。
为了避免这种情况,我想要一个 ProgressBar,它的出现只是为了让用户知道发生了一些事情,并且应用程序尚未锁定!
这是我的代码:
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 the login server stuff
// the problem is that thenew visibility doesn't happen until the login() is called...
}
};
我已经订购了代码,以便它使提交不可见,然后使进度条可见,然后进行登录(因此当应用程序连接到登录服务器时进度条将在那里旋转)。然而,它并没有达到我的意图——它似乎跳过了 setVisibility 代码,只调用了登录方法。它必须设置提交和进度栏项目的可见性,但它不会在登录方法执行其操作之前发生,因为它只是像平常一样锁定,并且在登录方法完成之前这些项目实际上永远不会隐藏/显示!
任何帮助将不胜感激。对不起论文!
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 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改 ProgressBar 可见性后,您需要将控制权交还给 UI 线程,并在另一个线程上执行登录工作。最好的方法是通过 AsyncTask。
这个答案有一个代码示例,用于在后台执行任务时显示 ProgressDialog。您可以修改它以在 ProgressBar 上调用
setVisibility
。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.尽管登录部分位于 UI 元素“之后”,但它仍然接管 UI 线程。
您应该尝试在单独的线程或 AsyncTask 中运行它。对于初学者来说,尝试一下这个(用它替换您的
login()
调用),然后如果它有效的话,让它变得更漂亮:您使用实际的整数而不是可用于设置可见性的标志有什么特殊原因吗?看起来像这样阅读会更容易:
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: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: