进度条在android中不起作用

时间:2014-07-18 08:38:16

标签: android android-handler

MainActivity Code

    import android.support.v4.app.Fragment;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    public class MainActivity extends Activity {

        private Button Button1, Button2;
        ProgressBar progressBar;

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

            Button1 = (Button) findViewById(R.id.button1);
            Button2 = (Button) findViewById(R.id.button2);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);

            Button2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "I am working",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        public void buttonClick(View v) {
            Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {        
                    progressBar.setProgress(msg.arg1);
                }
            };

        }
    }

AnotherClass代码

import android.os.Handler;
import android.os.Message;

public class AnotherExample {

Handler mhandler = new Handler();

public void testMethod() {
    for (int i = 0; i <= 50; i++) {
        Message obj1 = Message.obtain();        
        obj1.arg1 = i;
        mhandler.sendMessage(obj1);
    }
}

}

activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ThreadExampleActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="48dp"
        android:text="1st button" 
        android:onClick="buttonClick"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:text="2nd button" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:max="50"/> 


</RelativeLayout>    

我的应用程序上有两个按钮。我想打开进度条,点击button1,但是当我点击按钮1时,它没有显示任何进度。我已尝试在xml中使用android:indeterminate="true",但该时间进度条在不点击button1的情况下持续运行。

但我想点击button1,它会显示正在运行的进度条。 专家我需要你的帮助和建议我该怎么做。

1 个答案:

答案 0 :(得分:1)

更改您的代码,

public void buttonClick(View v) {
    Message message = new Message();
    message.arg1=10;
    message.what = 0;
    handler.sendMessage(message);

    }
Handler handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {        
           progressBar.setProgress(msg.arg1);
      }
};