Android:错误检查单选按钮文本值

时间:2017-01-13 22:02:05

标签: android

我正在尝试使用if语句根据所选单选按钮查看特定图标。 我不想使用听众,因为我还是点击了提交按钮。

我有3个按钮:Party,Accident和Other。 运行我的代码时,我的日志消息显示我总是将“other”(else子句)作为选中的值,即使它不是。

我已经从Stack Overflow上的许多不同线程尝试了很多不同的方法,但我担心我可能会做其他错误的事情。 这是代码:

    private void addMarkerToMap() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
    View view = getLayoutInflater().inflate(R.layout.dialoglayout, null);

    streetname = (EditText) view.findViewById(R.id.streetname_input);
    title = (EditText) view.findViewById(R.id.title_input);
    description = (EditText) view.findViewById(R.id.description_input);
    radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
    final int selectedID = radioGroup.getCheckedRadioButtonId();
    radioButton = (RadioButton) view.findViewById(selectedID);

    Button addButton = (Button) view.findViewById(R.id.add_button);
    Button cancelButton = (Button) view.findViewById(R.id.cancel_button);

    builder.setView(view);
    final AlertDialog dialog = builder.create();
    dialog.show();

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                addMarker(streetname, title, description);
        }
    });

    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
}

private void addMarker(EditText streetname, EditText title, EditText description) {
    String streetnameText = streetname.getText().toString();
    String titleText = title.getText().toString();
    String descriptionText = description.getText().toString();
    String radioText = radioButton.getText().toString();
    BitmapDescriptor icon; //= BitmapDescriptorFactory.fromResource(R.drawable.standard); //has to have a standard value
    //Log.d("id", "id" + ID);
    Log.d("radioText", "text: " + radioText);

    if(streetnameText.isEmpty() || titleText.isEmpty() || descriptionText.isEmpty()) {
        Toast.makeText(this, "One of the textboxes is empty. Please fill in all textboxes", Toast.LENGTH_LONG).show();
    } else {
        if(radioText.equals("Accident")){
            icon = BitmapDescriptorFactory.fromResource(R.drawable.accident);
        }
        else if(radioText.equals("Party")){
            icon = BitmapDescriptorFactory.fromResource(R.drawable.party);
        }
        else {
            icon = BitmapDescriptorFactory.fromResource(R.drawable.standard);
        }



        MarkerOptions markerOptions = new MarkerOptions().position(latlngGlobal)
                .title(titleText)
                .snippet(descriptionText)
                .icon(icon);

        //TODO add new marker
        mMarker =  mGoogleMap.addMarker(markerOptions);

        // TODO Get location for marker, add to database
    }
}

我也尝试过使用它,但它的结果相同(给出了“其他”的ID):

           if(ID == R.id.accident_radio){
            icon = BitmapDescriptorFactory.fromResource(R.drawable.accident);
        }
        else if(ID == R.id.party_radio){
            icon = BitmapDescriptorFactory.fromResource(R.drawable.party);
        }
        else {
            Log.d("id", "id" + ID);
            icon = BitmapDescriptorFactory.fromResource(R.drawable.standard);
        }

1 个答案:

答案 0 :(得分:1)

您在此特定时间将radioButton声明为群组中的所选单选按钮,在调用addMarker(...)之前,您不会为其执行任何新声明,因此它将始终相同。

而不是:

String radioText = radioButton.getText().toString();

尝试这样的事情(但要做得更好):

String radioText = ((RadioButton) view.findViewById(((RadioGroup) view.findViewById(R.id.radioGroup)).getCheckedRadioButtonId())).getText().toString();