如何使用 onCheckboxClicked 在 android studio 中获取复选框值

How to get checkbox values in android studio with onCheckboxClicked

私有 CheckBox 通道;

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

    register = findViewById(R.id.btn_register);
    ch = findViewById(R.id.checkboxCh);

    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            JSONObject jsonObject = new JSONObject();
            RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);

            try{
                jsonObject.put("ch", ch);
            }catch (JSONException exception){
                exception.printStackTrace();
            }

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, REGISTER_URL, jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println(response);
                    Toast.makeText(RegisterActivity.this, "You have registered", Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    Toast.makeText(RegisterActivity.this, "An error occurred", Toast.LENGTH_SHORT).show();
                }
            });

            //add request to the requestQueue
            requestQueue.add(jsonObjectRequest);
        }
    });

}

public void onCheckboxClicked(View view){
    //Is the view now checked?
    Boolean checked = ((CheckBox) view).isChecked();

    //Check if checkbox is selected or not
    if(ch.isChecked()){
        //checked=true;
    }else{
        //checked=false;
    }
}

我正在尝试使用 JSONObject 将布尔值 onCheckBoxClicked 的正确或错误答案发送到我的数据库。但是我在 if 语句中尝试的任何代码都是错误的。你有什么建议吗?我会很感激的。谢谢!

也许你可以添加新变量,比如 boolean result = false;,然后添加

public void onCheckboxClicked(View view){
//Is the view now checked?
Boolean checked = ((CheckBox) view).isChecked();

//Check if checkbox is selected or not
if(view.getId() == ch){
   result = checked;
}

}

并将行 jsonObject.put("ch", ch); 更改为 jsonObject.put("ch", result);

希望能解决您的问题,谢谢

这是正确答案,以防其他人遇到同样的问题...

private CheckBox ch;

private Boolean checked;

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

register = findViewById(R.id.btn_register);
ch = findViewById(R.id.checkboxCh);

register.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        JSONObject jsonObject = new JSONObject();
        RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);

        try{
            jsonObject.put("ch", checked);
        }catch (JSONException exception){
            exception.printStackTrace();
        }

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, REGISTER_URL, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                System.out.println(response);
                Toast.makeText(RegisterActivity.this, "You have registered", Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                Toast.makeText(RegisterActivity.this, "An error occurred", Toast.LENGTH_SHORT).show();
            }
        });

        //add request to the requestQueue
        requestQueue.add(jsonObjectRequest);
    }
});

}

public void onCheckboxClicked(View view){
    //Is the view now checked?
    checked = ((CheckBox) view).isChecked();

//Check if checkbox is selected or not
if(ch.isChecked()){
    checked=true;
}else{
    checked=false;
}

}