基于两个TextField动态更改ProgressBar的状态

时间:2019-01-12 09:12:47

标签: java android progress-bar android-studio-3.0

我正在尝试在用户在任一文本字段上键入文本时更改ProgressBar的状态。

例如,用户开始在第一个TextView上键入内容,ProgressBar应该更改其值,如果他删除了文本,则应该重置为之前的值是30

package com.example.phill.jokes;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Entry extends AppCompatActivity {
private ProgressBar pr;

private Handler rHandler = new Handler();
private TextView tv ;
private TextView tv2 ;
private boolean titleboolean;
private boolean jokeboolean;

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

    pr = (ProgressBar) findViewById(R.id.progressBar);
    tv=(TextView) findViewById(R.id.textView9) ;
    tv2=(TextView) findViewById(R.id.textView6) ;

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true)
            {

                android.os.SystemClock.sleep(300);
                rHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        pr.setProgress(30);
                        if(tv.getText().toString().matches(""))
                        {
                            titleboolean=true;
                        }
                        else
                            titleboolean=false;
                        if(tv2.getText().toString().matches(""))
                        {
                            jokeboolean=true;
                        }
                        else
                            jokeboolean=false;
                        if(titleboolean)
                        {
                            pr.setProgress(60);
                        }
                        if(jokeboolean)
                        {
                            pr.setProgress(100);
                        }

                    }
                });
                if(titleboolean && jokeboolean)
                    break;
            }
            rHandler.post(new Runnable() {
                @Override
                public void run() {
                    if(titleboolean && jokeboolean){
                        tv2.setVisibility(View.INVISIBLE);
                        tv.setVisibility(View.VISIBLE);
                    }
                    ;
                }
            });

        }
    }).start();
  }
}

ProgressBar的值永不变。 有任何想法吗?

2 个答案:

答案 0 :(得分:0)

让我详细解释一下

if(titleboolean && jokeboolean)
                break;

当两个文本字段都清除时,线程停止运行后,它将调用break语句,您可以通过另一种方式实现此目的

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TestActivity extends AppCompatActivity 
{
private ProgressBar pr;

private EditText tv;
private EditText tv2;
private boolean titleboolean;
private boolean jokeboolean;

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

    pr=(ProgressBar)findViewById(R.id.progressBar);
    tv = (EditText) findViewById(R.id.tv);
    tv2 = (EditText) findViewById(R.id.tv2);


    TextView tV = new TextView(this); 
    tV.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence 
  s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, 
    int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            setProgress();

        }
    });
    tv2.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence 
 s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, 
int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            setProgress();

        }
    });
}

public void setProgress() {
    pr.setProgress(30);
    if (tv.getText().toString().matches("")) {
        titleboolean = true;
    } else
        titleboolean = false;
    if (tv2.getText().toString().matches("")) {
        jokeboolean = true;
    } else
        jokeboolean = false;
    if (titleboolean) {
        pr.setProgress(60);
    }
        if (jokeboolean) {
            pr.setProgress(100);
        }
    }
}

答案 1 :(得分:0)

EditTextTextView一起使用。

  1. TextChangedListener不允许键入文本。
  2. 带有暂停的无限循环也是不好的方法,更改后请使用TextView处理文本