gpt4 book ai didi

android - EditText 的 TextWatcher 验证器

转载 作者:太空狗 更新时间:2023-10-29 14:34:11 25 4
gpt4 key购买 nike

我正在做一个登录 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 的新实例。我在泄漏内存吗?

最佳答案

While the AsyncTask is working I want to show a ProgressBar inside the EditText

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

And I am also a bit worried about creating new instances of the AsyncTask. Am I leaking memory?

嗯,您不能重用 AsyncTask 实例——它们是一次性的。请记住,AsyncTask 使用线程池,因此您可能同时执行多个 AsyncTasks,这可能与您的想法不符。我会考虑等到没有输入,比如 1 秒钟,然后再执行 Web 服务。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com