這是一件事情,我是Android新手。我正在使像Facebook一樣的嚮導註冊,我有5個活動。第一次活動從第五次活動中獲得響應
月1日活動 - 輸入電子郵件, 第二個活動 - 輸入名稱, 3日活動 - 輸入電話號碼, 4日活動 - 輸入密碼, 5日活動 - 顯示進度,同時與Web服務連接到存儲用戶在服務器端。
到目前爲止這麼好。
但是我想當服務器返回「有這封電子郵件的用戶存在」時,回到第一個活動並提示用戶輸入其他電子郵件,當用戶點擊提交時,而不是去第二個活動,重新發送到服務器端。
這裏是我的代碼:
1日活動
public class SignUp2stp extends Activity{
private Button button;
private EditText email;
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private Pattern pattern;
private Matcher matcher;
private static final int REQUEST_CODE = 1;
private Users users = null;
private int emailExist = 0;
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
checkFieldForEmptyValue();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up2stp);
email = (EditText) findViewById(R.id.email);
email.setText(getEmail());
email.addTextChangedListener(textWatcher);
}
public String getEmail(){
String possibleEmail = null;
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(SignUp2stp.this).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
possibleEmail = account.name;
}
}
return possibleEmail;
}
public void signUpActivity3stp(View view){
email = (EditText)findViewById(R.id.email);
pattern = Pattern.compile(EMAIL_PATTERN);
TextView emailErrorHandle = (TextView)findViewById(R.id.emailErrorHandle);
if(validateEmail(email.getText().toString())){
startActivity(new Intent(SignUp2stp.this,SignUp3stp.class).putExtra("email",email.getText().toString()));
}else{
//Handle invalid email error
email.setError("O seu email deve ser no formato: [[email protected]]");
}
}
public boolean validateEmail(String mail){
matcher = pattern.matcher(mail);
return matcher.matches();
}
public void checkFieldForEmptyValue(){
button = (Button)findViewById(R.id.btn_continue_a2);
email = (EditText)findViewById(R.id.email);
if(email.getText().toString().trim().isEmpty()){
button.setEnabled(false);
}else{
button.setEnabled(true);
}
}
}
5日活動
public class SaveUserDB extends Activity {
private ProgressBar spinner;
private Users users;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_user_db);
spinner = (ProgressBar)findViewById(R.id.spinner);
users = (Users) getIntent().getExtras().getSerializable("users");
new AsyncrTask().execute();
}
private class AsyncrTask extends AsyncTask<Void, Void, Integer>{
@Override
protected Integer doInBackground(Void... params){
try{
return new UserDAO().signUp(users);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
protected void onPreExecute(){
spinner.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected void onPostExecute(Integer result){
Intent intent = new Intent(SaveUserDB.this, SignUp2stp.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putSerializable("users",users);
intent.putExtras(bundle);
if(result == 0){
startActivity(intent);
}else if(result == 1){
//User email exists. Go to 1st activity Prompt user to enter other email
}else if (result == 2){
//User phone exists. Prompt user to enter other phone number
}else{
// Success and go to login activity
}
spinner.setVisibility(View.GONE);
super.onPostExecute(result);
}
}
}
「重新發送到服務器端」意味着「去第五個活動」? – Jerry