当前位置:操作系统 > 安卓/Android >>

Android新手入门教程(六):理解Activityの显示“进度条”对话框

当要进行耗时的操作的时候,往往会看见“请稍候”字样的对话框。例如,用户正在登入服务器,此时并不允许用户使用这个软件,或者应用程序把结果返回给用户之前,要进行某些耗时的计算。在这些情况下,显示一个“进度条”对话框,能友好地让用户等待,同时也能阻止用户进行某些不必要的操作。

    1.创建一个名为Dialog的工程。


    2.main.xml中的代码。


[java] <?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/btn_dialog2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:onClick="onClick2" 
        android:text="Click to display a progress dialog" /> 
 
</LinearLayout> 
<?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/btn_dialog2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick2"
        android:text="Click to display a progress dialog" />

</LinearLayout>    3.DialogActivity.java中的代码。


[java] package net.horsttnann.Dialog; 
 
import net.horsttnann.Dialog.R; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.view.View; 
 
public class DialogActivity extends Activity { 
    ProgressDialog progressDialog; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
    public void onClick2(View v) { 
        // ---show the dialog---  
        final ProgressDialog dialog = ProgressDialog.show(this, 
                "Doing something", "Please wait...", true); 
        new Thread(new Runnable() { 
            public void run() { 
                try { 
                    // ---simulate doing something lengthy---  
                    Thread.sleep(5000); 
                    // ---dismiss the dialog---  
                    dialog.dismiss(); 
                } catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } 
            } 
        }).start(); 
    } 

package net.horsttnann.Dialog;

import net.horsttnann.Dialog.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;

public class DialogActivity extends Activity {
    ProgressDialog progressDialog;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClick2(View v) {
        // ---show the dialog---
        final ProgressDialog dialog = ProgressDialog.show(this,
                "Doing something", "Please wait...", true);
        new Thread(new Runnable() {
            public void run() {
                try {
                    // ---simulate doing something lengthy---
                    Thread.sleep(5000);
                    // ---dismiss the dialog---
                    dialog.dismiss();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
    4.按F11调试,点击按钮,弹出“进度条”对话框。

 


效果图:

  \


 

提示:

   

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,