[英]how to make the screen dark and start a progressBar in android?
我正在编写一个Android应用程序,我想在执行httpclient.execute()
时,屏幕变暗,并且在执行此行之前会出现一个进度条,我该怎么办? 我的代码是这样的:
ProgressBar x = (ProgressBar) findViewById("R.id.progress") ;
x.setVisibility(ProgressBar.VISIBLE) ;
httpclient.execute()
x.setVisibility(ProgressBar.GONE) ;
但使用此代码只会出现进度条,屏幕不会变暗。
首先,我建议您在Async Class中进行所有网络连接。 您可以使用以下模板将代码放入Async Class。 将ProgressDialog作为类变量。
YouClass
{
ProgressDialog dialog;
onCreate(....)
{
//Execute the async task here.
new myNetworkingTask().execute("");
}
private class myNetworkingTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params)
{
//In this method you will do the networking task
httpclient.execute();
}
return "";
}
@Override
protected void onPostExecute(String result) {
//In this method you will hide the progress bar
dialog.dismiss();
}
@Override
protected void onPreExecute() {
//In this method you will display the progress bar.
dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
}
@Override
protected void onProgressUpdate(Void... values) {
}
如果您的HTTPClient在Asynctask中,请将您的进度条可见性GONE放在'onPostExecute'中
要调暗屏幕,您需要显示警告对话框/对话框片段并使用进度条设置自定义布局,然后在加载完成时将其关闭。
这将自动调暗屏幕。
在asynctas中添加httpclient.execute()部分..
在preexecute方法中添加pretask并在postexecute方法中发布任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.