我正在登录Activity,它基本上有一个EditText和两个Buttons
看起来是这样的:
http://img249.imageshack.us/img249/9108/loginm.png http://img249.imageshack.us/img249/9108/loginm.png
EditText具有一个TextWatcher来验证用户是否具有点击web服务的AsyncTask
代码:

public class Login extends Activity {

    private EditText mUserNameET;
    private Drawable mCorrect;
    private Drawable mIncorrect;
    private ProgressBar mProgressBar;
    private MyApp mApp;

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

        mApp = (MyApp) getApplication();
        mUserNameET = (EditText) findViewById(R.id.user_name_et);
        mUserNameET.addTextChangedListener(new LoginValidator());

        mCorrect = getResources().getDrawable(R.drawable.correct);
        mIncorrect = getResources().getDrawable(R.drawable.incorrect);
        mProgressBar = new ProgressBar(this);
    }

    private class LoginValidator implements TextWatcher {

        private ValidateUserAsyncTask validator = new ValidateUserAsyncTask();

        @Override
        public void afterTextChanged(Editable s) {

            String value = s.toString();

            if ( value.length() == 0 )
                return;

            /* If it's running, cancel it. */
            /* Issue #2 */
            if ( AsyncTask.Status.RUNNING == validator.getStatus() ) {
                validator.cancel(true);
                validator = new ValidateUserAsyncTask();
            } else if ( AsyncTask.Status.FINISHED == validator.getStatus() ) {
                validator = new ValidateUserAsyncTask();
            }

            validator.execute(value);
            /* Issue #1 */
            mUserNameET.setCompoundDrawables(null, null, mProgressBar.getProgressDrawable(), null);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

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

    class ValidateUserAsyncTask extends AsyncTask<String, Void, Boolean> {

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            if ( result ) {
                mUserNameET.setCompoundDrawablesWithIntrinsicBounds(null, null, mCorrect, null);
            } else {
                mUserNameET.setCompoundDrawablesWithIntrinsicBounds(null, null, mIncorrect, null);
            }
        }

        @Override
        protected Boolean doInBackground(String... params) {
            return mApp.validateUser(params[0]);
        }
    }
}

我的问题:
AsyncTask工作时,我想在EditText中显示一个progressbar。
我试着用:
mUserNameET.setCompoundDrawables(null, null, mProgressBar.getProgressDrawable(), null);

但没用。
我也有点担心创建AsyncTask的新实例。我是在泄露记忆吗?

最佳答案

当异步任务工作时

编辑文本
尝试创建一个表示进度指示器的AnimationDrawable,并将其与setCompoundDrawables()一起使用。
我也有点担心
创建的新实例
异步任务。我是在泄露记忆吗?
好吧,你不能重用AsyncTask实例——它们是一个完整的实例。请记住,AsyncTask使用一个线程池,因此您可能一次执行多个AsyncTasks,这可能是您想要的,也可能不是您想要的。我会考虑等到没有输入,比如说,1秒之后再做web服务。

关于android - EditText的TextWatcher验证器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3232395/

10-13 04:29