제목 그대로입니다.
현재 이름,성별, 수신여부를 받고 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();
}
}
}