마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

receive.isEmpty() 적용이 안됩니다.

0 추천

제목 그대로입니다.

현재 이름,성별, 수신여부를 받고 3개중에 한개라도 써있지않거나 체크가 되어있지 않으면 warning라는 내용을 TOAST로 출력하는 모양인데, 이름이 비면 warning라고 나오는데 이름을 쓰면 나머지 2개의 라디오박스중 하나만 선택해도 alter문으로 넘어갑니다..

if뒤에 조건이 잘못된걸까요

package com.dongyang.computer.myapplication6;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText etName;
    RadioButton rbMale, rbFemale;
    RadioButton rbPositive, rbNegative;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        setTitle("Customer Registration");

        etName = (EditText) findViewById(R.id.etName);

        rbMale = (RadioButton) findViewById(R.id.rbMale);
        rbFemale = (RadioButton) findViewById(R.id.rbFemale);

        rbNegative = (RadioButton) findViewById(R.id.rbNegative);
        rbPositive = (RadioButton) findViewById(R.id.rbPositive);
    }


    public void register(View v) {

        final String name = etName.getText().toString();


        final String gender;
        if (rbMale.isChecked()) {
            gender = rbMale.getText().toString();
        } else {
            gender = rbFemale.getText().toString();
        }

        final String receive;
        if (rbPositive.isChecked()) {
            receive = rbPositive.getText().toString();
        } else {
            receive = rbNegative.getText().toString();
        }


        if (name.isEmpty() || gender.isEmpty() || receive.isEmpty()) {

            Toast.makeText(MainActivity.this, "warning", Toast.LENGTH_SHORT).show();

        } else {

            AlertDialog.Builder alert = new AlertDialog.Builder(this);

            alert.setTitle("Notice");

            String message = "Name:" + name + "\n"
                    + "Gender:" + gender + "\n"
                    + "Receive:" + receive;

            alert.setMessage(message);

            alert.setIcon(R.mipmap.ic_launcher);

            alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intent = new Intent(MainActivity.this, SubActivity.class);


                    startActivity(intent);

                }
            });

            alert.setNegativeButton("NO", null);

            alert.show();
        }
    }
}




















익명사용자 님이 2016년 10월 14일 질문
2016년 10월 14일 수정

1개의 답변

0 추천

로직을 다시 한번 잘 생각해 보시기 바랍니다.

name.isEmpty()  => 이름이 입력되어 있지 않거나 체크가 되어 있지 않다

gender.isEmpty() => 성별이 입력되어 있지 않거나 체크가 되어 있지 않다

receive.isEmpty() => 수신여부가 입력되어 있지 않거나 체크가 되어 있지 않다

3개중에 한개라도 써있지않거나 체크가 되어있지 않으면 

=> if (name.isEmpty() || gender.isEmpty() || receive.isEmpty())

 

이름은 입력하지만 나머지 2개의 라디오박스중 하나만 선택하면

=> else

spark (227,930 포인트) 님이 2016년 10월 15일 답변
...