EditText的TextWatcher验证器

时间:2010-07-12 21:26:01

标签: android

我正在登录Activity,其基本上有EditText和两个Buttons。 它看起来像这样:

http://img249.imageshack.us/img249/9108/loginm.png http://img249.imageshack.us/img249/9108/loginm.png

EditText有一个TextWatcher,用于验证用户点击网络服务的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的新实例。我泄漏了记忆吗?

2 个答案:

答案 0 :(得分:1)

  

AsyncTask工作正常   在里面显示一个ProgressBar   的EditText

尝试创建代表进度指示器的AnimationDrawable,并将其与setCompoundDrawables()一起使用。

  

我也有点担心   创建新的实例   的AsyncTask。我泄漏了记忆吗?

好吧,你不能重用AsyncTask个实例 - 它们是一劳永逸的。请记住AsyncTask使用线程池,因此您可能同时执行多个AsyncTasks,这可能是您想到的,也可能不是。在做Web服务之前,我会考虑等到1秒没有输入。

答案 1 :(得分:0)

参考这篇文章它会有所帮助..虽然已经为一个按钮完成了,你可以为EditText更改它..

http://iserveandroid.blogspot.com/2011/01/how-to-bring-spinner-at-end-of-button.html

http://iserveandroid.blogspot.com/2011_01_01_archive.html