进度条不起作用

时间:2012-11-09 05:19:16

标签: android-widget

我是android的新手,我想创建一个需要进度条的应用程序,但我写的代码似乎每次运行时崩溃。至于现在我无法找到bug。我正在使用垂直进度条。

提前完成。

package com.example.progtask;

import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements OnClickListener, Runnable {
    private ProgressBar pb;
    private Button show;
    private Button hide;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = (ProgressBar) findViewById(R.id.progressBar1);
        show = (Button) findViewById(R.id.button1);
        hide = (Button) findViewById(R.id.hide);
        // pb.setVisibility(ProgressBar.GONE);
        show.setOnClickListener(this);
        hide.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.equals(show)) {

            pb.setVisibility(ProgressBar.VISIBLE);
            pb.setProgress(0);
            pb.setMax(12);
            new Thread(this).start();
        } else {
            pb.setVisibility(ProgressBar.GONE);
        }

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        int n = 0;
        while (n < 10) {
            n++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        pb.setProgress(n);

    }

}

以下是Xml文件

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="picelate"
        tools:context=".MainActivity" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="82dp"
        android:layout_toLeftOf="@+id/textView1"
        android:text="show" />

    <Button
        android:id="@+id/hide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/textView1"
        android:text="hide" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignParentLeft="true" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

试试这个

        @Override
        public void onClick(View v) {
           switch (v.getId())
        {
           case R.id.button1:

            pb.setVisibility(ProgressBar.VISIBLE);
            pb.setProgress(0);
            pb.setMax(12);
            new Thread(this).start();
           break;

          case R.id.hide:
         // do the needful.
         break;
        }

    }

答案 1 :(得分:0)

这就是它的工作原理:

 public class MainActivity extends Activity implements OnClickListener, Runnable {

    private ProgressBar pb;
    private Button show;
    private Button hide;
int n;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = (ProgressBar) findViewById(R.id.progressBar1);
        show = (Button) findViewById(R.id.button1);
        hide = (Button) findViewById(R.id.button2);
        // pb.setVisibility(ProgressBar.GONE);
        show.setOnClickListener(this);
        hide.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         switch (v.getId())
            {
               case R.id.button1:

                pb.setVisibility(ProgressBar.VISIBLE);
                pb.setProgress(0);
                pb.setMax(100);
                n = 0;
                new Thread(this).start();
               break;

              case R.id.button2:
             // do the needful.
                  pb.setVisibility(ProgressBar.GONE);
             break;
            }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        int n = 0;
        while (n < 100) {
            n++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }pb.setProgress(n*10);

        }
        pb.setProgress(n*10);

    }


}

xml文件是:

<LinearLayout 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" >

      <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="hide" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      android:progressDrawable="@drawable/progbar"
      android:layout_gravity="bottom"
        />

</LinearLayout>