【问题标题】:Issues with Radio buttons in RadioGroup in androidandroid中RadioGroup中的单选按钮问题
【发布时间】:2014-04-21 10:41:55
【问题描述】:

我的活动有一个带有两个单选按钮和保存按钮的 RadioGroup,它在字符串变量“type”中保存一个值。第一个单选按钮将类型初始化为“禁食”,第二个按钮将“类型”初始化为“非禁食”。如果我选择第二个选项,则类型不会被初始化。要使第二个按钮起作用,我必须选择第一个按钮并单击保存,然后如果我再次选择第二个选项,那么它将起作用。这是代码。

diabetes_options.xml

<RadioGroup
    android:id="@+id/do_radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/do_editText1"
    android:visibility="visible" >

    <RadioButton
        android:id="@+id/do_fasting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fasting" />

    <RadioButton
        android:id="@+id/do_nonfasting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Non Fasting" />
</RadioGroup>

DiabetesOptions.java(保存按钮的 onClick 方法内)

public class DiabetesOptions extends Activity {

int typeFlag=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    rGroup=(RadioGroup)findViewById(R.id.do_radioGroup1);
}
//This is the method invoked if we click Sabe button
public void onSave(View view) {

    rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            switch(checkedId){
            case R.id.do_fasting:
                type="Fasting";
                typeFlag=1;
                break;
            case R.id.do_nonfasting:
                type="Non Fasting";
                typeFlag=1;
                break;
            }
        }
    });

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    date = dateFormat.format(new Date());
    time = timeFormat.format(new Date());
    try {
        if(flag==1) {
        DiabetesDatabase entry = new DiabetesDatabase(this);
         if(typeFlag==1){
            entry.createEntry(date,time,type,level);
            Toast th = Toast.makeText(DiabetesOptions.this, "Entry Saved", Toast.LENGTH_SHORT);
            th.show();
         }
         else if(typeFlag==0){
            AlertDialog.Builder alertDialog=new AlertDialog.Builder(DiabetesOptions.this);
            alertDialog.setTitle("Alert!!!");
            alertDialog.setMessage("Select FASTING or NON FASTING option");
            alertDialog.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dialog.dismiss();                        
                   }
               });
            alertDialog.show();
         }
        }

}

【问题讨论】:

  • 你能显示你设置typeFlag的代码吗?
  • @Szymon 我编辑了上面的代码
  • flag 在哪里声明?我只能看到它使用了if(flag==1)
  • flag 是在 typeFlag 之后声明的。但这不是问题的原因。 onCheckedChanged 只有在我选择了第一个选项后才起作用。然后点击保存并返回并选择第二个选项。如果我在第一次尝试中选择第二个选项,它就不起作用

标签: android android-radiogroup android-radiobutton


【解决方案1】:

您会遇到这种情况,因为 OnCheckedChangedListener 只有在您第一次单击“保存”按钮后才会注册。但那时将不再有您可以做出反应的 OnCheckedChanged 事件,因为在您单击按钮之前已经发生了。

您还可以在每次单击“保存”按钮时设置一个新的 OnCheckedChangedListener。我认为这不是你想要的。

onCreate 方法中初始化 RadioGroup 时设置 OnCheckedChangeListener。这应该够了吧。让我知道它是否有效。

【讨论】:

    猜你喜欢
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多