繁体   English   中英

为Android创建进度条

[英]Creating a progress bar for android

嗨,我是线程技术的新手,我正在尝试设置一个进度条,该进度条使用sleep方法每半秒增加一次,最多增加100。 我在使用很多在网上找到的语法时遇到了麻烦,当我启动该程序时,它不显示进度条,而是显示旋转的加载图标。 这就是我所拥有的:

public class main extends ActionBarActivity {
Handler mHandler;


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

    final Button startButton = (Button) findViewById(R.id.startbutton);

    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(1);
            startPb();
        }
    });
}
private void startPb(){

    final ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar);
    pb.setIndeterminate(false);
    pb.setProgress(0);
    new Thread(new Runnable(){

        public void run(){
            for (int i = 0; i <= 100; i++)
                try {
                    Thread.sleep(500);
                    pb.setProgress(i);
                    System.out.println(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            pb.post(new Runnable() {
                public void run() {
                    pb.incrementProgressBy(1);
                }
            });

        }
    }).start();
}

我感觉上面的代码没有正确实现进度条。 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Counter"
android:id="@+id/counter"
tools:ignore="InvalidId">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="startButton"
    android:id="@+id/startbutton"
    android:layout_centerHorizontal="true" />

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_below="@+id/startbutton"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

旋转进度条是不确定的进度条。 要使用实际的进度条,您需要调用:

pb.setIndeterminate(false);

进度栏样式也是一个问题。 这是解释进度条样式类型的良好参考。 ProgressBar中的android:progressBarStyle属性是什么意思?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM