Android:进度栏对话框的代码

亚历克斯·伊拉伯(Alex Irabor)

我正在编写一个android程序来测试android中不同类型的对话框。除带有进度条的对话框外,其他所有选项均能正常工作。该代码有效,但进度条拒绝增加。我在下面有该应用程序的源代码,也许有人可以帮助我找出我做错了什么:

package com.example.progressdial;

//import com.example.alertdialogtest.R;

import android.support.v7.app.ActionBarActivity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        }


        //DIALOG NO 4
        //This method starts a progressBar Dialog
        public void startProgressBarDialog(View v){
            //create a new instance of the progressDialog class..
            final ProgressDialog progressBarDialog= new ProgressDialog(this);

            //set the icon, title and progress style..
            progressBarDialog.setIcon(R.drawable.ic_launcher);
            progressBarDialog.setTitle("Downloading files...");
            progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

            //set the OK and the CANCEL Buttons..

                //setting the OK Button
                progressBarDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,
                            int whichButton){
                        Toast.makeText(getBaseContext(),
                                "OK clicked!", Toast.LENGTH_SHORT).show();
                    }
                });

                //set the Cancel button
                progressBarDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int whichButton){
                        Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
                    }
                });

            //initialise the progressBar
            progressBarDialog.setProgress(0);
            new Thread(new Runnable(){
                public void run(){
                    for (int i=0; i<=15; i++){
                        try{
                            Thread.sleep(1000);
                            progressBarDialog.incrementProgressBy((int)(100/15));
                        }
                        catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                    //dismiss the dialog
                    progressBarDialog.dismiss();
                 }
               });
            //show the dialog
            progressBarDialog.show();
        }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
莱昂纳多

您忘记了启动线程。尝试在下面添加起点:

new Thread(new Runnable(){
            public void run(){
                for (int i=0; i<=15; i++){
                    try{
                        Thread.sleep(1000);
                        progressBarDialog.incrementProgressBy((int)(100/15));
                    }
                    catch(InterruptedException e){
                        e.printStackTrace();
                    }
                }
                //dismiss the dialog
                progressBarDialog.dismiss();
            }
        }).start(); <------------------------

看看是否有效

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

关于Android进度对话框。避免?

来自分类Dev

将进度对话框添加到操作栏中,从夏洛克片段

来自分类Dev

进度对话框中的“显示警报”对话框已完成android

来自分类Dev

android进度对话框-> publishProgress方法

来自分类Dev

进度对话框-AsyncTask

来自分类Dev

显示进度对话框,但不显示进度栏,并且消息未更新

来自分类Dev

Android:进度对话框显示黑屏

来自分类Dev

如何使进度对话框与更高版本的Android兼容

来自分类Dev

在对话框/弹出窗口上加载图像时,ANDROID进度对话框

来自分类Dev

Android asynctask不显示进度对话框

来自分类Dev

Android使进度对话框ProgressBar不可见

来自分类Dev

Android:使用AsyncTask的进度对话框

来自分类Dev

Android asynctask显示进度对话框,直到功能完成

来自分类Dev

无法关闭Android中的进度对话框

来自分类Dev

从对话框获取搜索栏进度

来自分类Dev

进度对话框Android

来自分类Dev

android进度对话框-> publishProgress方法

来自分类Dev

Android加载带有进度对话框的列表视图

来自分类Dev

方法未完成时的进度栏对话框

来自分类Dev

进度对话框-AsyncTask

来自分类Dev

Android:将等级栏值传递给对话框

来自分类Dev

Twitter异步任务中的进度对话框-Android

来自分类Dev

在活动显示进度对话框时阻止onClick(Android)

来自分类Dev

Android进度对话框重力问题

来自分类Dev

Android中进度对话框的文本对齐

来自分类Dev

等待对话框显示时的Android进度对话框

来自分类Dev

进度未在进度对话框 Android Studio 中增加

来自分类Dev

android中的进度对话框不会关闭

来自分类Dev

Xamarin 进度对话框

Related 相关文章

热门标签

归档