Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一个活动OTP验证中创建两个按钮setOnClickListener和onClick_Java_Android_Android Layout_Android Intent - Fatal编程技术网

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

Java 在一个活动OTP验证中创建两个按钮setOnClickListener和onClick,java,android,android-layout,android-intent,Java,Android,Android Layout,Android Intent,是否可以在两个不同的xml布局上的一个活动中创建两个按钮setOnClickListener和onClick?! 在这段代码中,我想通过RESTAPI注册一个用户,然后验证通过向他的手机号码发送短信,我如何在不使用意图的情况下在一个活动中做到这一点? 如果注册成功,它会将其发送到另一个xml版面,并要求输入opt code(发送到用户手机),但btn_opt_send在我单击它时不会执行任何操作 public class RegisterActivity extends BaseActivity

是否可以在两个不同的xml布局上的一个活动中创建两个按钮setOnClickListener和onClick?! 在这段代码中,我想通过RESTAPI注册一个用户,然后验证通过向他的手机号码发送短信,我如何在不使用意图的情况下在一个活动中做到这一点? 如果注册成功,它会将其发送到另一个xml版面,并要求输入opt code(发送到用户手机),但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)

为事件创建一个公共类,并将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());