在一个活动OTP验证中创建两个按钮setOnClickListener和onClick

时间:2015-11-15 10:58:37

标签: java android android-layout android-intent

是否可以在两个不同的xml布局上的一个活动中创建两个按钮setOnClickListener和onClick? 在此代码中,我想通过REST API注册用户,然后通过向他的手机号码发送短信来验证,如何在不使用意图和一项活动的情况下执行此操作? 如果注册成功,它会将其发送到另一个xml布局并要求输入选择代码(发送到用户手机),但是当我点击它时btn_opt_send什么都不做....

public class RegisterActivity extends BaseActivity implements OnClickListener {
    private WebView cweBview;
    private String fromLocation = "";
    private String destinationLocation = "";
    private Button btnTopBack;
    private TextView lblHeader;

    private EditText txtName;
    private EditText txtPassword;
    private EditText txtPasswordConfirm;
    private EditText txtEmail;
    private EditText txtPhone;
    private EditText txtFullName;
    private EditText txtAddress;
    private EditText opt_code;
    private TextView btnRegister, lblConfirmPass, lblPassword, btn_opt_send;
    private boolean isLogin;
    private User user;
    public static final String MY_PREFS_NAME = "LucasApp";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        user = new User();
        isLogin = GlobalValue.getInstance().isLogin;
        if (isLogin) {
            user = GlobalValue.getInstance().currentUser;
    //      btn_opt_send.setOnClickListener(this);
        }
        initUI();
        initControl();
        getData();
    }

    private void initUI() {
        lblHeader = (TextView) findViewById(R.id.lblHeaderTtle);
        btnTopBack = (Button) findViewById(R.id.btnLeft);
        btnTopBack.setBackgroundResource(R.drawable.btn_back);
        btnTopBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onBackPressed();
            }
        });

        txtName = (EditText) findViewById(R.id.txtName);
        txtPassword = (EditText) findViewById(R.id.txtPassword);
        txtPasswordConfirm = (EditText) findViewById(R.id.txtPasswordConfirm);
        txtEmail = (EditText) findViewById(R.id.txtEmail);
        txtPhone = (EditText) findViewById(R.id.txtPhone);
        txtFullName = (EditText) findViewById(R.id.txtFullName);
        txtAddress = (EditText) findViewById(R.id.txtAddress);
        btnRegister = (TextView) findViewById(R.id.btnRegister);
        lblConfirmPass = (TextView) findViewById(R.id.lblConfirmPass);
        lblPassword = (TextView) findViewById(R.id.lblPassword);
        opt_code = (EditText) findViewById(R.id.opt_code);

        if (isLogin) {
            lblHeader.setText(getString(R.string.lbl_my_profile));
            txtName.setText(user.getUser_name());
            txtEmail.setText(user.getEmail());
            txtPhone.setText(user.getPhone());
            txtFullName.setText(user.getFull_name());
            txtAddress.setText(user.getAddress());
            txtPasswordConfirm.setVisibility(View.GONE);
            lblConfirmPass.setVisibility(View.GONE);
            txtPassword.setVisibility(View.GONE);
            lblPassword.setVisibility(View.GONE);

            txtName.setEnabled(false);
            txtEmail.setEnabled(false);
            lblHeader.setText(getString(R.string.lbl_update_profile));
            btnRegister.setText(getString(R.string.lbl_update_profile));
        } else {
            lblHeader.setText(getString(R.string.lbl_register));
            btnRegister.setText(getString(R.string.lbl_register));
        }
    }

    private void initControl() {
        btnRegister.setOnClickListener(this);
    //  btn_opt_send.setOnClickListener(this);
    }

    private void getData() {

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btnRegister:
            if (isLogin) {
                updateProfile();
            } else {
                register();
            }
            break;
            case R.id.btn_opt_send:
                if (isLogin) {
                    updateProfile(); }
                else {
                opt();
            }
                break;
        }
    }


    public void opt() {
        // user.setUser_name(txtName.getText().toString());
        user.setOpt(opt_code.getText().toString());
        ModelManager.needOPT(this, true, user.getUser_name(),
                user.getOpt(), new ModelManagerListener()  {

                        public void onSuccess(String json) {
                        // TODO Auto-generated method stub
                        if (ParserUtility.isSuccess(json)) {
                            user = ParserUtility.parseUser(json);
                            GlobalValue.getInstance().setCurrentUser(
                                    ParserUtility.parseUser(json));
                                    GlobalValue.getInstance().setLogin(true);
                            showToastMessage(getResources().getString(
                                    R.string.register_success));

                            SharedPreferences mPrefs = getSharedPreferences(
                                    MY_PREFS_NAME, MODE_PRIVATE);
                            Editor prefsEditor = mPrefs.edit();
                            Gson gson = new Gson();
                            String jsonTemp = gson.toJson(ParserUtility
                                    .parseUser(json));
                            prefsEditor.putString("MyObject", jsonTemp);
                                    prefsEditor.putBoolean("isLogin", true);
                            prefsEditor.commit();
                            onBackPressed();
                        } else {
                            showToastMessage(getResources().getString(
                                    R.string.register_failed)
                                    + ParserUtility.getMessage(json));
                        }
                    }
                    @Override
                    public void onError() {
                        // TODO Auto-generated method stub
                        showToastMessage(getResources().getString(
                                R.string.have_some_error));
                    }

                });
    }
    public void register() {
        // Get value from view
        user.setUser_name(txtName.getText().toString());
        user.setPassword(txtPassword.getText().toString());
        user.setEmail(txtEmail.getText().toString());
        user.setPhone(txtPhone.getText().toString());
        user.setFull_name(txtFullName.getText().toString());
        user.setAddress(txtAddress.getText().toString());
        String passWordConfirm = txtPasswordConfirm.getText().toString();

        if (user.getUser_name().isEmpty() || user.getPassword().isEmpty()
                || user.getEmail().isEmpty() || user.getPhone().isEmpty()
                || user.getFull_name().isEmpty() || user.getAddress().isEmpty()
                || passWordConfirm.isEmpty()) {
            showToastMessage(getResources().getString(
                    R.string.lbl_please_enter_full));
            return;
        }

        if (!StringUtility.isEmailValid(user.getEmail())) {
            showToastMessage(getResources().getString(
                    R.string.email_is_not_validated));
            return;
        }

        if (!user.getPassword().equals(passWordConfirm)) {
            showToastMessage(getResources().getString(
                    R.string.passwords_do_not_match));
            return;
        }

        ModelManager.register(this, true, user.getUser_name(),
                user.getPassword(), user.getEmail(), user.getFull_name(),
                user.getPhone(), user.getAddress(), new ModelManagerListener() {
                    @Override
                    public void onSuccess(String json) {
                        // TODO Auto-generated method stub
                        if (ParserUtility.isSuccess(json)) {
                            user = ParserUtility.parseUser(json);
                            GlobalValue.getInstance().setCurrentUser(
                                    ParserUtility.parseUser(json));
                    //      GlobalValue.getInstance().setLogin(true);
                            showToastMessage(getResources().getString(
                                    R.string.register_success_needopt));

                            SharedPreferences mPrefs = getSharedPreferences(
                                    MY_PREFS_NAME, MODE_PRIVATE);
                            Editor prefsEditor = mPrefs.edit();
                            Gson gson = new Gson();
                            String jsonTemp = gson.toJson(ParserUtility
                                    .parseUser(json));
                            prefsEditor.putString("MyObject", jsonTemp);
                    //      prefsEditor.putBoolean("isLogin", true);
                            prefsEditor.commit();
                        //  onBackPressed();
                            setContentView(R.layout.opt_req);

                        } else {
                            showToastMessage(getResources().getString(
                                    R.string.register_failed)
                                    + ParserUtility.getMessage(json));
                        }
                    }

                    @Override
                    public void onError() {
                        // TODO Auto-generated method stub
                        showToastMessage(getResources().getString(
                                R.string.have_some_error));
                    }
                });
    }


    }
}

第二个布局名称是R.layout.opt_req,按钮位于该布局上(btn_opt_send)

1 个答案:

答案 0 :(得分:0)

为事件创建一个公共类,并将onclicklistener设置为该自定义类。或者你可以使用一个活动和片段,以便你的服务器通信应该在活动中,你的布局将在片段上 创建一个这样的类

public class CustomClickLister implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.yourButton:
                //your logic
                breake;
        }
    }
}

像这样设置onclick监听器

 yourButton.setOnClickListener(new CustomClickLister());