我目前正在处理我的登录活动,我的电子邮件验证遇到了麻烦。即使toast消息确实说“无效的电子邮件地址”,它仍然会转到下一个Activity而不是保留在LoginActivity中,直到它是有效的电子邮件地址。
这是我的LoginActivity.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.facebook.widget.LoginButton;
import com.wny.wecare.handler.JSONParser;
public class LoginActivity extends Activity implements OnClickListener {
public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
"[a-zA-Z0-9+._%-+]{1,256}" +
"@" +
"[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
"(" +
"." +
"[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
")+"
);
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputEmail;
Button btnLogin;
public String strUid;
// url to create new user
private static String url_create_user = "http://infinitycodeservices.com/create_user.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private LoginButton btnFacebookLogin;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
// Edit Text
inputEmail = (EditText) findViewById(R.id.email);
// Create button
Button btnCreateUser = (Button) findViewById(R.id.btnEmail);
// button click event
btnCreateUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email=inputEmail.getText().toString();
if(checkEmail(email))
Toast.makeText(LoginActivity.this,"Valid Email Addresss", Toast.LENGTH_SHORT).show();
else
Toast.makeText(LoginActivity.this,"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
// creating new user in background thread
new CreateNewUser().execute();
}
});
}
private boolean checkEmail(String email) {
return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}
/**
* Background Async Task to Create new User
* */
class CreateNewUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Logging In..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating User
* */
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Setup ArrayList from main activity to store results
ArrayList<Map<String, String>> emailList = new ArrayList<Map<String, String>>();
String email = ((EditText) findViewById(R.id.email)).getText().toString().trim();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Email", email));
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// Getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url_create_user, params);
for (int i = 0; i < json.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
try {
JSONObject c = (JSONObject) json.get(i);
//Fill map
Iterator iter = c.keys();
while(iter.hasNext()) {
String currentKey = (String) iter.next();
map.put(currentKey, c.getString(currentKey));
}
emailList.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
};
//SAVE
SharedPreferences ui = getSharedPreferences("UserInfo", MODE_PRIVATE);
SharedPreferences.Editor edUi = ui.edit();
edUi.putString("uid", strUid);
edUi.putString("email", email);
edUi.commit();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
// Returns Toast "Login success!"
//return json.getString(TAG_MESSAGE);
return null;
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(LoginActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
在真实条件中执行你的asynctask
if(checkEmail(email)) {
Toast.makeText(LoginActivity.this,"Valid Email Addresss", Toast.LENGTH_SHORT).show();
// creating new user in background thread
new CreateNewUser().execute();
}
else {
Toast.makeText(LoginActivity.this,"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
如果条件如下,你只需要将花括号括起来: -
// button click event
btnCreateUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email=inputEmail.getText().toString();
if(checkEmail(email)){
Toast.makeText(LoginActivity.this,"Valid Email Addresss",Toast.LENGTH_SHORT).show();
// creating new user in background thread
new CreateNewUser().execute();
}else{
Toast.makeText(LoginActivity.this,"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
}
}
});
代码问题在于您每次都在执行异步任务。