选中时如何使用截击发送单选按钮/单选组的数据

ulydmbyx  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(425)

我正在为我正在创建的应用程序做一个注册表。它包括名字,姓氏,电子邮件,电话号码,密码,出生日期和性别。单选组中的性别,显然是使用单选按钮。我用截击来连接数据。如何使用截击传递选中的单选按钮值?
以下是我迄今为止的java代码:

public class B_Register extends AppCompatActivity implements View.OnClickListener {

public static final String REGISTER_URL = "http://10.0.0.245/register_alex's_app.php";

public static final String FNAME_REGISTER = "first_name";
public static final String LNAME_REGISTER = "last_name";
public static final String EMAIL_REGISTER = "email_register";
public static final String USERNAME_REGISTER = "username_register";
public static final String PHONE_NUMBER_REGISTER = "phone_register";
public static final String PASSWORD_REGISTER = "password_register";
public static final String BDAY_REGISTER = "bday_register";

private EditText fname;
private EditText lname;
private EditText email;
private EditText phone_number;
private EditText new_username;
private EditText new_password;
private EditText bday;

private RadioGroup gender;

private Button register_btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.b_register);

    fname = (EditText)findViewById(R.id.first_name_et);
    lname = (EditText)findViewById(R.id.last_name_et);
    email = (EditText)findViewById(R.id.email_et);
    phone_number = (EditText)findViewById(R.id.phone_et);
    new_username = (EditText)findViewById(R.id.username_et);
    new_password = (EditText)findViewById(R.id.password_et);
    bday = (EditText)findViewById(R.id.bday_et);

    gender = (RadioGroup)findViewById(R.id.gender_register);

    register_btn = (Button)findViewById(R.id.register_btn);
    register_btn.setOnClickListener(this);

}

public void onTermsOfUseClick(View view) {
}

@Override
public void onClick(View view) {

    final String firstName = fname.getText().toString().trim();
    final String lastName = lname.getText().toString().trim();
    final String new_email = email.getText().toString().trim();
    final String newPhoneNumber = phone_number.getText().toString().trim();
    final String userName = new_username.getText().toString().trim();
    final String newPassword = new_password.getText().toString().trim();
    final String bDay = bday.getText().toString().trim();

    if(TextUtils.isEmpty(firstName)) {
        fname.setError("Please enter your first name.");
        return;
    }else if(TextUtils.isEmpty(lastName)) {
        lname.setError("Please enter your last name.");
        return;
    }else if(TextUtils.isEmpty(new_email)) {
        email.setError("Please enter your email.");
        return;
    }else if(TextUtils.isEmpty(newPhoneNumber)) {
        phone_number.setError("Please enter your phone number.");
        return;
    }else if(TextUtils.isEmpty(userName)) {
        new_username.setError("Please enter a valid username, 5-25 characters.");
        return;
    }else if (TextUtils.isEmpty(newPassword)) {
        new_password.setError("Please enter valid password, 5-15 characters.");
    }else if (TextUtils.isEmpty(bDay)) {
        bday.setError("Please enter your birth day.");
    }
}
}
neekobn8

neekobn81#

直接访问单选按钮,而不是radiogroup。

RadioButton male = (RadioButton) findViewById(R.id.male);
boolean isMale = male.isChecked();

记住给单选按钮贴上标签。

<RadioGroup
    android:id="@+id/gender_register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_male"
        android:checked="true" />

    <RadioButton
        android:id="@+id/female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_female" />

</RadioGroup>
ve7v8dk2

ve7v8dk22#

了解所选的 RadioButton 身份证检查:

// get selected radio button from radioGroup
 int selectedId = radioGroup.getCheckedRadioButtonId();
 // find the radiobutton by returned id
 radioButton = (RadioButton) findViewById(selectedId);
 String radio_value = radioButton.getText(); //by this you will get the male or female text which will be used in sending to server.

使用发送数据 Volley ,添加依赖项:

compile 'com.android.volley:volley:1.0.0'

向服务器发送数据的截取方法:

try {
                // initialize request to connect the server, via POST method and Server URl
                StringRequest stringRequest = new StringRequest(Request.Method.POST, your_url,
                        new Response.Listener<String>() {
                            // catch JSON response from server
                            @Override
                            public void onResponse(final String response) {
                                // response from server
                            }
                        },

                        // catch error response from server
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(final VolleyError error) {
                                // trace error message from server 
                            }
                        }){

                    @Override
                    protected Map<String,String> getParams(){
                        Map<String,String> params = new HashMap<String, String>();
                        // map the string value, to send in server
                        params.put("name",string_value_of_name);
                        params.put("sex",string_value_of_sex);
                        .... //like more
                        return params;
                    }
                };

                // execute the request
                RequestQueue requestQueue = Volley.newRequestQueue(your_activity_context.this);
                requestQueue.add(stringRequest);

            }catch (final Exception e){
                //catch exception
            }

如需更多详细信息,请访问链接
注:此示例仅用于演示,不包含用户要求的确切答案,必须使用所示流程/逻辑应用您的想法。

相关问题