我创建了以下警告对话框和进度条:
LayoutInflater layoutInflater = LayoutInflater.From(this);
View progressDialogBox = layoutInflater.Inflate(Resource.Layout.progress_dialog_box, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.SetView(progressDialogBox);
var progressBar1 = progressDialogBox.FindViewById<ProgressBar>(Resource.Id.progressBar1);
progressBar1.Max = 100;
progressBar1.Progress = 0;
Dialog dialog = alertDialogBuilder.Create();
dialog.Show();
我每2秒钟增加一次
System.Threading.Thread.Sleep(2000);
progressBar1.IncrementProgressBy(25);
System.Threading.Thread.Sleep(2000);
progressBar1.IncrementProgressBy(25);
System.Threading.Thread.Sleep(2000);
progressBar1.IncrementProgressBy(25);
System.Threading.Thread.Sleep(2000);
progressBar1.IncrementProgressBy(25);
progress_dialog_box.axml包含该代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Getting data..."
android:layout_gravity="center"
android:paddingTop="40dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar1" />
</LinearLayout>
但是当我运行应用程序时,ProgressBar保持为0.如何才能使其进展?
答案 0 :(得分:2)
但是当我运行应用程序时,ProgressBar保持为0。
它是100而不是0. progressBar1.IncrementProgressBy(25);
将在对话框显示之前被调用(我还没有找到为什么在对话框显示之前已经完成了该过程,如果我找到,我将更新我的解决方案)
这是一个简单的:
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
LayoutInflater layoutInflater = LayoutInflater.From(this);
View progressDialogBox = layoutInflater.Inflate(Resource.Layout.progress_dialog_box, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.SetView(progressDialogBox);
var progressBar1 = progressDialogBox.FindViewById<ProgressBar>(Resource.Id.progressBar1);
progressBar1.Max = 100;
progressBar1.Progress = 0;
Dialog dialog = alertDialogBuilder.Create();
dialog.Show();
UpdatePB uptask = new UpdatePB(this, progressBar1);
uptask.Execute(100);
}
public class UpdatePB : AsyncTask<int, int, string>
{
Activity mcontext;
ProgressBar mpb;
public UpdatePB(Activity context, ProgressBar pb)
{
this.mcontext = context;
this.mpb = pb;
}
protected override string RunInBackground(params int[] @params)
{
// TODO Auto-generated method stub
for (int i = 1; i <= 4; i++)
{
try
{
System.Threading.Thread.Sleep(3000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
Android.Util.Log.Error("lv", e.Message);
}
mpb.IncrementProgressBy(25);
PublishProgress(i * 25);
}
return "finish";
}
protected override void OnProgressUpdate(params int[] values)
{
Android.Util.Log.Error("lv==", values[0] + "");
}
protected override void OnPostExecute(string result)
{
mcontext.Title = result;
}
}
}
并将ProgressBar
的宽度更改为match_parent
,这样会更好。