2017-07-28 995 views
62

我遇到過ProgressDialog已被棄用。除了ProgressBar之外,還有其他替代選擇。 我正在使用android studio版本2.3.3。ProgressDialog不推薦使用。替代方法是什麼?

ProgressDialog progressDialog=new ProgressDialog(this); 
progressDialog.show(); 
+0

請我answer.i希望這將幫助你 –

+0

[ProgressDialog可能的重複被棄用](https://stackoverflow.com/questions/45351127/progressdialog-is-deprecated) –

回答

3

由於在documentation page替代提到的是ProgressBarProgressDialog的外觀可以通過將ProgressBar放入AlertDialog來複制。

你仍然可以使用它,但Android不希望你使用它,這就是它被棄用的原因。所以你應該考慮以另一種方式解決你的問題,比如將ProgressBar嵌入你的Layout

+0

因爲我們仍然可以使用它,它會崩潰在api級別26+還是會正常工作(與較低的api相同)? –

15

此類在API級別26中已棄用。ProgressDialog是一種模式 對話框,可防止用戶與應用程序進行交互。取而代之的是使用此類的 ,您應該使用進度指示器,如 ProgressBar,它可以嵌入到應用程序的用戶界面中。或者, 您可以使用通知來通知用戶任務的進度。 link

它在Android O因爲Google新的UI標準

+1

「此類在API級別26中已棄用」,而不是日期。他們不希望你再使用它。是的,這是由於新的UI標準,但它們適用於每個API級別...... – creativecreatorormaybenot

+3

「由於Google新的UI標準,它在Android O中已棄用」 - 請鏈接新的UI標準 –

3

也許這guide可以幫助你過時了。

通常我更喜歡使用指示器來定製AlertDialogs。它解決了諸如App視圖自定義之類的問題。

79

是的,API level 26已棄用,建議您只效法progressbar

使用此代碼,用於創建編程:

RelativeLayout的佈局= findViewById(R.id。顯示); //在這裏指定根佈局ID

(或)

RelativeLayout的佈局= findViewById(本);

progressBar = new ProgressBar(youractivity.this,null,android.R.attr.progressBarStyleLarge); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100); 
params.addRule(RelativeLayout.CENTER_IN_PARENT); 
layout.addView(progressBar,params); 
progressBar.setVisibility(View.VISIBLE); //To show ProgressBar 
progressBar.setVisibility(View.GONE);  // To Hide ProgressBar 

要禁用,你只需要添加以下 代碼的用戶交互

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, 
          WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 

得到用戶的互動活動,你只需要添加以下代碼

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

只是以供將來參考,改變android.R.attr.progressBarStyleSmallandroid.R.attr.progressBarStyleHorizontal

此以下行代碼的工作僅在高於21 API progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));


解決方案2:另一種方法

<ProgressBar 
     android:id="@+id/progressbar" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:indeterminate="true" 
     android:max="100" 
     android:backgroundTint="@color/white" 
     android:layout_below="@+id/framelauout" 
     android:indeterminateTint="#1a09d6" 
     android:layout_marginTop="-7dp"/> 

Activity.java

progressBar = (ProgressBar) findViewById(R.id.progressbar); 
`progressBar.setVisibility(View.VISIBLE);`// To Show ProgressBar 
`progressBar.setVisibility(View.INVISIBLE);` //To Hide ProgressBar 

欲瞭解更多詳情:1.Reference one 2.Reference Two

我希望這可以幫助你。

+0

謝謝大家的upvote。 –

+0

爲第一種方法,如何以編程方式添加或顯示progressBar – usman

+1

@usman請檢查我的更新答案...如果它是有用的donot忘記upvote。 –

8

那麼,如果你真的想要去違背自己的意願,仍然可以使用Sweet Alert Dialog或創建一個你自己的。

progress_dialog_layout 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TableRow 
     android:layout_centerInParent="true" 
     android:layout_width="match_parent" 
     android:layout_height="64dp" > 

     <ProgressBar 
      android:id="@+id/progressBar2" 
      style="?android:attr/progressBarStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" /> 

     <TextView 
      android:gravity="center|left" 
      android:id="@+id/textView9" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textColor="@color/black" 
      android:textSize="18sp" 
      android:text="Downloading data. Please wait.." /> 
    </TableRow> 
</RelativeLayout> 

Java代碼:

AlertDialog b; 
AlertDialog.Builder dialogBuilder; 

public void ShowProgressDialog() { 
dialogBuilder = new AlertDialog.Builder(DataDownloadActivity.this); 
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View dialogView = inflater.inflate(R.layout.progress_dialog_layout, null); 
      dialogBuilder.setView(dialogView); 
      dialogBuilder.setCancelable(false); 
      b = dialogBuilder.create(); 
      b.show(); 
     } 

     public void HideProgressDialog(){ 

      b.dismiss(); 
     } 
+1

我在SweetAlertDialog的製作環境中遇到過可怕的經歷,因爲它在窗口之間泄漏,所以非常謹慎。 – Jeremy

1

它可以幫助其他人。

很多熱門的應用有不同的方法來顯示的像網絡請求,文件加載等加載微調什麼進展沒有顯示多少內容已經被加載或剩餘加載。有一段時間的不確定性是壞UI/UX的觀點。很多流行的應用程序(Facebook,Linkedin等)已經通過首先顯示裸露的骨骼UI顯示解決了這個問題。然後,加載的內容逐漸填充在屏幕上。

我已經使用shimmer爲我的應用程序來解決這個問題。

有關於這這將是其他人

1

我用DelayedProgressDialog從https://github.com/Q115/DelayedProgressDialog它確實一樣ProgressDialog與延遲額外的好處,如果必要的有益的好article

使用它的是AndroidØ前同樣ProgressDialog:

DelayedProgressDialog progressDialog = new DelayedProgressDialog(); 
progressDialog.show(getSupportFragmentManager(), "tag"); 
1

ProgressDialog API等級26已被否決。

"Deprecated"指的是在由較新的被替換的過程中的功能或元件。

ProgressDialog是一個模態對話框,這防止用戶從與 該應用進行交互。您應該使用像ProgressBar這樣的進度 指標,而不是使用此課程,該指標可以嵌入您應用的用戶界面 。

優勢

我個人說,ProgressBar擁有兩個邊緣。 ProgressBar是指示操作進度的用戶界面元素。以非中斷的方式向用戶顯示進度條。在應用的用戶界面中顯示進度條。

5

您可以簡單地爲您的進度條設計一個xml接口,並將其作爲視圖傳遞給AlertDialog,然後隨時顯示或關閉對話框。

progress.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:padding="13dp" 
    android:layout_centerHorizontal="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ProgressBar 
     android:id="@+id/loader" 
     android:layout_marginEnd="5dp" 
     android:layout_width="45dp" 
     android:layout_height="45dp" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:text="Loading..." 
     android:textAppearance="?android:textAppearanceSmall" 
     android:layout_gravity="center_vertical" 
     android:id="@+id/loading_msg" 
     android:layout_toEndOf="@+id/loader" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

顯示的進度對話框中的代碼的代碼。只需複製此代碼並將其粘貼到片段即可。

private void setDialog(boolean show){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     //View view = getLayoutInflater().inflate(R.layout.progress); 
     builder.setView(R.layout.progress); 
     Dialog dialog = builder.create(); 
     if (show)dialog.show(); 
     else dialog.dismiss(); 
    } 

,每當你想顯示progressdialog並通過真正作爲參數來顯示它或假關閉該對話框然後,只需調用該方法。

+0

無法解析獲取活動 –

+0

設置視圖需要API級別21.所以沒有準備好api19 –

+1

@Alok Rajasukumaran它似乎你正在使用活動,只需將它替換爲'this',以便它看起來像這樣'AlertDialog.Builder builder =新的AlertDialog.Builder(this);'。請確保你正在複製正確的代碼。 – DevMike

相關問題