Android百議程序:進度條對話方塊實現

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   os   java   for   

顯示由Activity管理的dialog。

這種dialog有多種多樣,其中比較常見的是loading的時候,顯示的一個loading進度條。

Android顯示這樣的進度條還是非常方便的,因為有現成的模組可以調用。

首先看看本程式的效果吧:

1 主介面:


2 點擊這個按鈕之後,顯示:


進度條到了100的時候就會自動關閉,當然這裡是類比下載,真實的下載演算法還需要繼續完善,不過也是很簡單的演算法了,不算是痛點。

點擊Cancel或者OK按鈕也可以調用函數,進行有需要的操作,這裡直接顯示一個Toast(簡單短暫的對話方塊)提示。不了。


步驟:

建立項目之後,用以下代碼做出主介面:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/dialog_button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:onClick="onClick"        android:text="@string/dialog_button" />  </LinearLayout>

然後是邏輯代碼:

package su.dialog.dialog;import android.app.Activity;import android.app.Dialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {ProgressDialog progressDialog;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void onClick(View v) {showDialog(1);progressDialog.setProgress(0);new Thread(new Runnable() {public void run() {for (int i = 1; i <= 15; i++) {try {Thread.sleep(1000);progressDialog.incrementProgressBy((int) (100.f / 15.f));} catch (InterruptedException e) {e.printStackTrace();}}progressDialog.dismiss();}}).start();}@Overrideprotected Dialog onCreateDialog(int id) {progressDialog = new ProgressDialog(this);progressDialog.setIcon(R.drawable.ic_launcher);progressDialog.setTitle("Downloading files...");progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Toast.makeText(getBaseContext(), "OK clicked!",Toast.LENGTH_SHORT).show();}});progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Toast.makeText(getBaseContext(), "Cancel clicked!",Toast.LENGTH_SHORT).show();}});return progressDialog;}}

showDialog()函數已經是deprecated的函數了,可以參照:http://stackoverflow.com/questions/10285047/showdialog-deprecated-whats-the-alternative


知識點:

1 點擊事件是在xml中聲明的了,就是這句:android:onClick = "onClick";後面的"onClick"就是自己定義的函數,可以為任意名字,只要xml的名字和邏輯代碼中的名字一致就可以驅動點擊事件了。

2 模組ProgressDialog類有點複雜,其實也是一些屬性的設定和函數調用罷了,理解起來不難,細心看代碼就行了。




Android百議程序:進度條對話方塊實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.