在安卓中确定性进度对话框

6

我希望使用一个有确定进度条的ProgressDialog而不是默认的旋转指示器。看起来很简单:ProgressDialog提供了setIndeterminate方法,并且show方法接受一个布尔值表示是否为不确定性。但是这些方法对我都不起作用!对话框仍然是不确定性的并带有旋转指示器。如何改变这种行为呢?

3个回答

10
你需要调用:

You need call:

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

请查看官方开发指南中“显示进度条”的部分,其中还附有示例源代码。


0
Oh Sorry.. Try this

1) Create a layout file with below code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/RelativeLayout_DownloadProgress" android:paddingLeft="20dp" android:paddingBottom="20dp" android:paddingRight="10dp" android:paddingTop="20dp">
    <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/downloading_video_tv" android:text="@string/downloading_video" android:textStyle="bold" android:textColor="@color/COLOR_WHITE"></TextView>
    <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/download_progress_tv" android:text="@string/zero_percent" android:layout_below="@+id/downloading_video_tv" android:textColor="@color/COLOR_WHITE"></TextView>

    <Button
        android:id="@+id/download_cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/downloading_video_tv"
        android:text="@string/Cancel"
        android:textColor="@color/COLOR_CANCEL_BTN" android:layout_marginLeft="5dp">
</Button>

   <ProgressBar
       android:id="@+id/download_progressbar"
       style="?android:attr/progressBarStyleHorizontal"
       android:layout_width="fill_parent"
       android:layout_height="13dp"
       android:layout_below="@id/download_progress_tv"
       android:layout_toLeftOf="@id/download_cancel_btn"
       android:indeterminateOnly="false"
       android:max="100"
       android:progressDrawable="@drawable/dwnld_progress" >

</ProgressBar>


</RelativeLayout>



2) In your code do the following

    i) in oncreate() method add the following lines..
      //Download progress layout inflate
       RelativeLayout relativelayout_DownloadProgress = (RelativeLayout)_inflater.inflate(R.layout.downloadprogress, null);
       TextView downloadProgress_PercentageTV = (TextView)relativelayout_DownloadProgress.findViewById(R.id.download_progress_tv);
       Button downloadProgress_CancelBtn = (Button)relativelayout_DownloadProgress.findViewById(R.id.download_cancel_btn);
      ProgressBar  download_progressBar = (ProgressBar)relativelayout_DownloadProgress.findViewById(R.id.download_progressbar);
        downloadProgress_CancelBtn.setOnClickListener(new OnClickListener() {        
            public void onClick(View v) {
                // TODO Auto-generated method stub  

                dismissDialog(0);
            }
        });
    ii)  //Creating Dialogs i.e., below onCreate() method
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            return new AlertDialog.Builder(_activity)
            .setView(relativelayout_DownloadProgress)
            .create();
        }//switch end
        return null;
    }//onCreateDialog()

   iii) on any click event add the following lines. 
     showDialog(0);download_progressBar.setProgress(0);


Sorry i know its too long code but its working code in application...Try it might need modifications like variables must be declared in class scope.

这段代码使用的是 ProgressBar,而不是 ProgressDialog。我希望能够通过 ProgressDialog 实现这样的行为,只有在无法实现时才会按照您建议的方式进行。 - aplavin
是的,它使用了ProgressBar,但该视图显示为对话框,因此不应该有任何问题。在我目前正在开发的应用程序中,同样的代码也可以正常工作。 - Ishu

-3
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Registering. Please wait...");
dialog.setIndeterminate(true); 
dialog.setCancelable(false); 
dialog.show(); 

Try this code should work fine...

1
我觉得你误解了我的意思:我需要一个确定的进度条,显示出已完成的进程有多少。 - aplavin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接