简体   繁体   English

Android 进度条不起作用

[英]Android Progress Bar not working

I have made a progress bar in Android but it is not at all working.我在 Android 中制作了一个进度条,但它根本不起作用。 What am I doing wrong?我究竟做错了什么? I have just started to learn android.我刚开始学习安卓。 Below is my code.下面是我的代码。

MainActivity.java- MainActivity.java-

public class MainActivity extends AppCompatActivity {
    ProgressBar progressBar;
    int mporgress=0;
    EditText time;
    private Handler mHandler = new Handler();
    private static final int PROGRESS = 0x1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
           public void Startbuttononclick(View view){
               Button startbutton=(Button) findViewById(R.id.button);
               startbutton.setOnClickListener(new View.OnClickListener(){
                   @Override
                   public void onClick(View v) {

                       mporgress = Integer.parseInt(time.getText().toString());
                       progressBar.setProgress(mporgress);

                       new Thread(new Runnable() {
                           public void run() {
                               while (mporgress < 100) {
                                   // Update the progress bar
                                   mHandler.post(new Runnable() {
                                       public void run() {
                                           progressBar.setProgress(mporgress);
                                           mporgress = doWork();
                                           try{
                                               Thread.sleep(1000);
                                           }catch (InterruptedException e){}
                                       }
                                   });
                               }
                           }
                       }).start();

                   }
               });
           }
    public void doprogress(View view) {

    }
        public int doWork(){
                  mporgress++;
            return mporgress;
        }
}

Activity_main.xml- Activity_main.xml-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.android.progressbar.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter the time:"
        android:inputType="number"
        android:id="@+id/editText" />

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="500dp"
        android:layout_height="200dp"
        android:id="@+id/progressBar4"
        android:layout_gravity="center"
        android:scrollbarSize="500dp" />

    <Button
        android:text="START"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_gravity="center"
        android:onClick="Startbuttononclick" />
</LinearLayout>

Check out this image below- - https://i.stack.imgur.com/2TBIb.png看看下面这张图片 - https://i.stack.imgur.com/2TBIb.png

Note- Enter the time in this image is an EditText with a hint and not a TextView.注意 - 在此图像中输入时间是带有提示的 EditText 而不是 TextView。

It is appearing but not working.它出现但不起作用。

I have done these changes so far.- https://i.stack.imgur.com/UujpB.png到目前为止,我已经完成了这些更改。- https://i.stack.imgur.com/UujpB.png

But still the progress bar is still continuously rotating.但进度条仍在不断旋转。

Seems you forgot to initialize EditText -time and ProgressBar .似乎您忘记初始化EditText -timeProgressBar Init it inside onCreate :onCreate初始化它:

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

    progressBar =(ProgressBar) findViewById(R.id.progressBar4);
    time = (EditText) findViewById(R.id.editText); //here
}

Also inside button click the bar :同样在按钮内部单击bar

progressBar = new ProgressBar(this);
mporgress = Integer.parseInt(time.getText().toString());
    ................
          .............

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

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