我使用Asynctasks验证EditTexts上的一些输入,问题是,即使asynctasks在doinbackground()部分,当我按下按钮时,它将转到下一个活动。我的问题是我使用asynctasks进行验证。
以下是我的Next Button代码:
@Override
public void onNextClicked(StepperLayout.OnNextClickedCallback callback) {
getData();
new VerifyAppNameTask().execute(sAppName);
f2 = edtAppCategoryET(sAppCategory);
f3 = edtAppPlatformET(sAppPlatform);
f4 = edtAppDescriptionET(sAppDescription);
f5 = edtAppVersionET(sAppVersion);
if (!f1 && !f2 && !f3 && !f4 && !f5)
{
bundleData = new Bundle();
bundleData.putString("appName", sAppName);
bundleData.putString("appCategory", sAppCategory);
bundleData.putString("appPlatform", sAppPlatform);
bundleData.putString("appVersion", sAppVersion);
bundleData.putString("appDescription", sAppDescription);
callback.goToNextStep();
}
else
{
displaySnackBar("Cannot proceed. Please fix the errors.").show();
}
}
对于Asynctask:
class VerifyAppNameTask extends AsyncTask<String, Void, String> {
// use doInBackground() to make network calls, the returned value is
// sent to onPostExecute()
@Override
protected String doInBackground(String... data) {
if (data[0].replace(" ","").isEmpty())
{
f1 = true;
return "1";
}
else if (data[0].length() > 25)
{
f1 = true;
return "2";
}
else if (checkAppName(data[0]))
{
f1 = true;
return "3";
}
else
{
f1 = false;
return "4";
}
}
@Override
protected void onPostExecute(String result) {
tilAppName.setErrorEnabled(true);
switch(result)
{
case "1": {tilAppName.setError("You can't leave this empty.");break;}
case "2": {tilAppName.setError("Maximum of 25 characters.");break;}
case "3": {tilAppName.setError("App Name already used.");break;}
case "4": {tilAppName.setError(null);tilAppName.setErrorEnabled(false);break;}
}
}
}
我需要的是当我点击下一个按钮时,它应该等待所有asynctasks先完成。
答案 0 :(得分:1)
f2 = edtAppCategoryET(sAppCategory);
f3 = edtAppPlatformET(sAppPlatform);
f4 = edtAppDescriptionET(sAppDescription);
f5 = edtAppVersionET(sAppVersion);
if (!f1 && !f2 && !f3 && !f4 && !f5)
{
bundleData = new Bundle();
bundleData.putString("appName", sAppName);
bundleData.putString("appCategory", sAppCategory);
bundleData.putString("appPlatform", sAppPlatform);
bundleData.putString("appVersion", sAppVersion);
bundleData.putString("appDescription", sAppDescription);
callback.goToNextStep();
}
else
{
displaySnackBar("Cannot proceed. Please fix the errors.").show();
}
在AsyncTask
的{{1}}中添加此代码,以便它可以提供帮助。在AsyncTask完成其任务之后,此代码将运行。希望它有所帮助
答案 1 :(得分:0)
试试这段代码:
StepperLayout.OnNextClickedCallback mCallback;
@Override
public void onNextClicked(StepperLayout.OnNextClickedCallback callback) {
mCallback = callback;
getData();
new VerifyAppNameTask().execute(sAppName);
}
private void yourMethod(){
f2 = edtAppCategoryET(sAppCategory);
f3 = edtAppPlatformET(sAppPlatform);
f4 = edtAppDescriptionET(sAppDescription);
f5 = edtAppVersionET(sAppVersion);
if (!f1 && !f2 && !f3 && !f4 && !f5)
{
bundleData = new Bundle();
bundleData.putString("appName", sAppName);
bundleData.putString("appCategory", sAppCategory);
bundleData.putString("appPlatform", sAppPlatform);
bundleData.putString("appVersion", sAppVersion);
bundleData.putString("appDescription", sAppDescription);
mCallback.goToNextStep();
}
else
{
displaySnackBar("Cannot proceed. Please fix the errors.").show();
}
}
class VerifyAppNameTask extends AsyncTask<String, Void, String> {
// use doInBackground() to make network calls, the returned value is
// sent to onPostExecute()
@Override
protected String doInBackground(String... data) {
if (data[0].replace(" ","").isEmpty())
{
f1 = true;
return "1";
}
else if (data[0].length() > 25)
{
f1 = true;
return "2";
}
else if (checkAppName(data[0]))
{
f1 = true;
return "3";
}
else
{
f1 = false;
return "4";
}
}
@Override
protected void onPostExecute(String result) {
tilAppName.setErrorEnabled(true);
switch(result)
{
case "1": {tilAppName.setError("You can't leave this empty.");break;}
case "2": {tilAppName.setError("Maximum of 25 characters.");break;}
case "3": {tilAppName.setError("App Name already used.");break;}
case "4": {tilAppName.setError(null);tilAppName.setErrorEnabled(false);break;}
}
yourMethod();
}
}
答案 2 :(得分:0)
这是我的样本
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (null != asyncTask && asyncTask.isFinished) {
startActivity(new Intent(MainActivity.this,
NextActivity.class));
}
}
});