radiobutton的变化没有登记

时间:2016-12-26 18:20:51

标签: java android xml android-layout radio-button

我有一个由片段组成的导航活动。目前,该应用程序用于从无线电组中获取所选的Id,并在点击按钮时将其显示为吐司。但应用程序只显示已经检查过的单选按钮(在xml中定义),如果我更改了所选的单选按钮,它仍然会显示在xml中定义的那个被检查的

这是java类(FirstFragment.java)

public class FirstFragment extends Fragment implements View.OnClickListener {

View myView;

private RadioGroup radioGroup;
private RadioButton radioButton;
Button btnDisplay;
String text;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myView = inflater.inflate(R.layout.first_layout, container, false);


    btnDisplay = (Button) myView.findViewById(R.id.btnDisplay);
    btnDisplay.setOnClickListener(this);

    radioGroup = (RadioGroup) myView.findViewById(R.id.radio);
    int selectedId = radioGroup.getCheckedRadioButtonId();
    radioButton = (RadioButton) myView.findViewById(selectedId);

    return myView;
}

@Override
public void onClick(View v) {
    Toast.makeText(getActivity(),
            text = radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
    }

}

这是xml文件

<RadioGroup
        android:id="@+id/radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="89dp">

        <RadioButton
            android:id="@+id/radioGram"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="Gram"/>

        <RadioButton
            android:id="@+id/radioKilogram"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Kilogram"
            android:textSize="16sp"
            android:checked="true"/>

    </RadioGroup>

所以,在这种情况下,吐司会显示“Kilogram”,但如果我检查“Gram”radiobutton,吐司仍会显示“Kilogram”。谁能帮到解决这个问题?谢谢

2 个答案:

答案 0 :(得分:1)

试试这个:

private RadioButton rb_1, rb_2;


 //in on createView
    rb_1 = (RadioButton) myView.findViewById(R.id.radioGram);
    rb_2 = (RadioButton) myView.findViewById(R.id.radioKilogram);

     rb_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),
            rb1.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        rb_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),
            rb2.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });

替代方法:在启动myView

后,在onCreateView()中写入此内容
View myView = inflater.inflate(R.layout.first_layout, container, false);
 RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radio);        
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // checkedId is the RadioButton selected
         switch(checkedId){
                case R.id.radioGram:
                    // do operations specific to this selection
                    Toast.makeText(getActivity(),"Gram selected", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.radioKilogram:
                    // do operations specific to this selection
                    Toast.makeText(getActivity(),"KiloGram selected", Toast.LENGTH_SHORT).show();
                    break;
            }  
    }
});

答案 1 :(得分:0)

您正在致电

$(document).ready(function() {   
    var options = {  
    beforeSubmit: showRequest,
    error:  showError,  // error
    success: showResponse  // post-submit callback 
}; 
$(document).on('submit', '#send-form', function(event) { 
               $('#send-form').ajaxForm(options);
               return false;
});
}); 

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
  alert('Please wait...','Please wait...');
  return true; 
} 
// error callback
function showError(er, err, error) {  
  var currentAlert = $.jAlert('current');
  currentAlert.closeAlert();
  errorAlert('Error','Try Again Later');
} 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
  var currentAlert = $.jAlert('current');
  currentAlert.closeAlert();
  successAlert('Success!', 'Your form has been sent');
} 

<form action="" method="post" id="send-form">
<input type="submit" class="submit-btn" name="modal-form" value="submit" />
</form>

在onCreateView里面只调用一次。因为每次在吐司上显示相同的值。尝试在onClick中获取价值:

int selectedId = radioGroup.getCheckedRadioButtonId();
    radioButton = (RadioButton) myView.findViewById(selectedId);