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

Dynamically change the status of a ProgressBar based on two TextFields

当用户在任一文本字段中键入文本时,我正在尝试更改 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 的值永远不会改变。 有什么想法吗?

我给你详细解释一下

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);
        }
    }
}

使用 EditText 代替 TextViewTextChangedListener

  1. TextView 不允许输入文本。
  2. 无限循环和停顿也是不好的方法,使用 EditText.addTextChangedListener 来处理更改后的文本