第1次活动得到第5次活动的回复

时间:2015-05-01 19:45:31

标签: android

这是一件事,我是android的新手。我正在制作像Facebook一样的向导注册表格,我有5个活动。

第一项活动 - 输入电子邮件, 第二个活动 - 输入名称, 第3项活动 - 输入电话号码, 第4项活动 - 输入密码, 第5个活动 - 在连接Web服务时显示进度,以便在服务器端存储用户。

到目前为止一切顺利。

但我希望当服务器返回"存在此电子邮件的用户"时,返回第1个活动并提示用户输入其他电子邮件,当用户点击提交时,而不是转到第2个活动,重新发送到服务器端。

这是我的代码:

第一项活动

    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: [user@dominio.com]");

        }
    }

    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);
        }
    }
}

第五项活动

    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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

启动活动时为intent设置标记或捆绑并检查它。您将了解此活动从哪种方法开始。但这不是一个正确的解决方案。使用fragmetns,它简化了你的代码和逻辑