我正在为我的Android应用程序制作一个注册屏幕。我已经知道如何设置用户名,密码和类似的东西。现在我有一个带有两个单选按钮的无线电组,用于选择性别。如何设置监听器以了解选择了哪个单选按钮,然后将所选性别上传到parse.com?这就是我到目前为止所做的:
protected EditText mUsername;
protected EditText mPassword;
protected EditText mEmail;
protected EditText mName;
protected RadioButton mFemaleButton;
protected RadioButton mMaleButton;
protected Button mSignUpButton;
protected Button mCancelButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_sign_up);
ActionBar actionBar = getActionBar();
actionBar.hide();
mUsername = (EditText) findViewById(R.id.usernameField);
mPassword = (EditText) findViewById(R.id.passwordField);
mEmail = (EditText) findViewById(R.id.emailField);
mName = (EditText) findViewById(R.id.first_lastname);
mFemaleButton = (RadioButton) findViewById(R.id.femaleButton);
mMaleButton = (RadioButton) findViewById(R.id.maleButton);
mCancelButton = (Button) findViewById(R.id.cancelButton);
mCancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
mSignUpButton = (Button) findViewById(R.id.signupButton);
mSignUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
String email = mEmail.getText().toString();
String name = mName.getText().toString();
username = username.trim();
password = password.trim();
email = email.trim();
name = name.trim();
if (username.isEmpty() || password.isEmpty() || email.isEmpty()
|| name.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(
SignUpActivity.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
// create the new user!
setProgressBarIndeterminateVisibility(true);
final ParseUser newUser = new ParseUser();
newUser.setUsername(username);
newUser.setPassword(password);
newUser.setEmail(email);
newUser.put("name", mName.getText().toString());
newUser.signUpInBackground(new SignUpCallback() {
@Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
TextlyApplication
.updateParseInstallation(ParseUser
.getCurrentUser());
Intent intent = new Intent(SignUpActivity.this,
MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
SignUpActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok,
null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
}
答案 0 :(得分:0)
试试这个:
maleRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
newUser.put(COLUMN_GENDER, MALE);
newUser.saveInBackground(); // if you want to save immediately
}
}
});
femaleRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
newUser.put(COLUMN_GENDER, FEMALE);
newUser.saveInBackground(); // if you want to save immediately
}
}
});
编辑回答:
添加此代码:
private static final String COLUMN_GENDER = ""; //put here name of your column from table on Parse.com
private static final String MALE = ""; //put here value for male. Check the type of gender column on Parse.com (String, Number (Integer), Boolean, etc.)
private static final String FEMALE = ""; //put here value for female. Check the type of gender column on Parse.com (String, Number (Integer), Boolean, etc.)
在您的行protected Button mCancelButton;
添加此代码:
String gender = mMaleButton.isChecked() ? MALE : FEMALE;
在你的行下面:
username = username.trim();
password = password.trim();
email = email.trim();
name = name.trim();
添加此代码:
newUser.put(COLUMN_GENDER, gender);
在你的行下面:
final ParseUser newUser = new ParseUser();
newUser.setUsername(username);
newUser.setPassword(password);
newUser.setEmail(email);
newUser.put("name", mName.getText().toString());