0

所以我基本上想知道如果填写了txtCode或StreetCode,我将如何允许用户在他们点击付款时继续到下一页,只需要填写这两个中的一个才能传递到下一页但是如何我让它工作,因为目前他们都必须填写才能传递到下一页。

    btnPay = (Button)findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
    btnPay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
            /** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
             txtReg = (EditText)findViewById(R.id.txtReg);
             txtCode = (EditText)findViewById(R.id.txtCode);
             txtStreetName = (EditText)findViewById(R.id.txtStreetName);

             dlCostTime = (Spinner)findViewById(R.id.dlCostTime);



            if( txtReg.getText().toString().trim().equals(""))
            {
                txtReg.setError("required!");
            }
            if( txtCode.getText().toString().trim().equals(""))
            {
                txtCode.setError("required!");
            }
            if( txtStreetName.getText().toString().trim().equals(""))
            {
                txtStreetName.setError("required!");
            }


            else{
                final Button btnPay = (Button) findViewById(R.id.btnPay);

                btnPay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
                        UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
                    }
                });

            }
4

2 回答 2

0

替换此代码

 btnPay = (Button) findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
    btnPay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
            /** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
            txtReg = (EditText) findViewById(R.id.txtReg);
            txtCode = (EditText) findViewById(R.id.txtCode);
            txtStreetName = (EditText) findViewById(R.id.txtStreetName);

            dlCostTime = (Spinner) findViewById(R.id.dlCostTime);


            if (txtCode.getText().toString().trim().length() > 0 || txtStreetName.getText().toString().trim().length() > 0) {
                final Button btnPay = (Button) findViewById(R.id.btnPay);

                btnPay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
                        UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
                    }
                });
            } else if (txtStreetName.getText().toString().trim().length() == 0) {
                txtStreetName.setError("required!");
            } else if (txtReg.getText().toString().trim().length() == 0) {
                txtReg.setError("required!");
            } else if (txtCode.getText().toString().trim().length() == 0) {
                txtCode.setError("required!");
            }
        }
    }
于 2017-03-31T11:08:06.030 回答
0

替换此代码

if( txtReg.getText().toString().trim().equals(""))
{
    txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
    txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
    txtStreetName.setError("required!");
}


else{
    final Button btnPay = (Button) findViewById(R.id.btnPay);

    btnPay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
            UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
        }
    });

}

有了这个

if( txtReg.getText().toString().trim().equals("")){
    txtReg.setError("required!");
}else{

    if((txtCode.getText().toString().trim().equals("") && !txtStreetName.getText().toString().trim().equals("")) || 
            (!txtCode.getText().toString().trim().equals("") && txtStreetName.getText().toString().trim().equals(""))){
        Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
        UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
    }else{
        //show message enter either code or street name not both
}
于 2017-04-01T13:11:20.697 回答