【问题标题】:Android : Help in RadioButton ToastAndroid : RadioButton Toast 中的帮助
【发布时间】:2020-01-11 02:44:05
【问题描述】:

如果用户在没有选择选项的情况下单击“下一步”按钮,它必须烤一条消息,“请选择任何一个”,否则它应该进入下一个屏幕。我已经尝试过,但它不会进入下一个屏幕,而是显示吐司“请选择任何一个”和我的代码

public class Question1 extends Activity implements OnClickListener {
Button vid, next;
Typeface tp;
TextView tv;
RadioGroup rg;
RadioButton rb;
Button b;
SharedPreferences sp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.question1);
    sp = getSharedPreferences("AppFile1", 0);

    tv = (TextView) findViewById(R.id.tvQuestion1);

    rg = (RadioGroup) findViewById(R.id.radioGroup1);
    vid = (Button) findViewById(R.id.buttonQuestion1VIdeo);
    next = (Button) findViewById(R.id.buttonQuestion1Next);
    vid.setOnClickListener(this);
    next.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.buttonQuestion1VIdeo:
        Intent i = new Intent(Question1.this, VideoActivity.class);
        startActivity(i);
        break;
    case R.id.buttonQuestion1Next:
        if (next.isSelected()) {
            int selectedId = rg.getCheckedRadioButtonId();
            rb = (RadioButton) findViewById(selectedId);
            String s = rb.getText().toString();
            SharedPreferences.Editor ed = sp.edit();
            ed.putString("q1", s);
            ed.commit();
            Intent ia = new Intent(Question1.this, Question2.class);
            startActivity(ia);

        } else {
            Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
        }

        break;
    default:
        break;
    }

}

}

【问题讨论】:

  • 调试它并应用断点并获取值并比较结果 1st

标签: android radio-button toast


【解决方案1】:

尝试:

if (rg.getCheckedRadioButtonId()!=-1) {
        int selectedId = rg.getCheckedRadioButtonId();
        rb = (RadioButton) findViewById(selectedId);
        String s = rb.getText().toString();
        SharedPreferences.Editor ed = sp.edit();
        ed.putString("q1", s);
        ed.commit();
        Intent ia = new Intent(Question1.this, Question2.class);
        startActivity(ia);

    } else {
        Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
    }

文档说如果没有选中任何按钮,getCheckedRadioButtonId() 将返回 -1

返回该组中选定单选按钮的标识符。空选时,返回值为-1。

因此,如果您从该函数中获得除 -1 之外的任何值,则您有一个选定的按钮。

【讨论】:

    【解决方案2】:

    错误条件if (next.isSelected())

    真实条件:您要确保必须选择选项:

    boolean checked = ((RadioButton) view).isChecked();
    

    【讨论】:

      【解决方案3】:

      您正在检查 Next 按钮本身的选择状态。 但是您想检查单选按钮选择状态。 所以你必须给它:

      if (rg.getCheckedRadioButtonId()!=-1) {
                  int selectedId = rg.getCheckedRadioButtonId();
                  rb = (RadioButton) findViewById(selectedId);
                  String s = rb.getText().toString();
                  SharedPreferences.Editor ed = sp.edit();
                  ed.putString("q1", s);
                  ed.commit();
                  Intent ia = new Intent(Question1.this, Question2.class);
                  startActivity(ia);
      
              } else {
                  Toast.makeText(getApplicationContext(), "Please Select Answer", 0).show();
              }
      

      【讨论】:

      • 意味着在不检查所有单选按钮的情况下,无法检查 RadioGroup 内是否选择了任何选项
      • @ρяσѕρєяK 不,也有可能。 :) 我的错 。编辑
      【解决方案4】:

      情况会是这样的

      rb1  = (RadioButton)findViewById(R.id.rb1);
      rb2  = (RadioButton)findViewById(R.id.rb2);
      rb3  = (RadioButton)findViewById(R.id.rb3);
      
      if(rb1.isChecked()==false && rb2.isChecked()==false && rb3.isChecked()==false)
      {
           // Toast message is here
      }
      else
      {
          // Intent code is here
      }
      

      【讨论】:

        【解决方案5】:
        RadioGroup rad_grp;
        Button clear,submit;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        clear = findViewById(R.id.clear);
        submit = findViewById(R.id.submit);
        rad_grp = findViewById(R.id.rad_grp);
        rad_grp.clearCheck();
        
        
        rad_grp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
        
                RadioButton rb = group.findViewById(checkedId);
                if (null != rb && checkedId > -1)
                {
                Toast.makeText(MainActivity.this,rb.getText(), Toast.LENGTH_SHORT).show();
                }
                }
        });
        
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rad_grp.clearCheck();
            }
        });
        
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioButton rb = rad_grp.findViewById(rad_grp.getCheckedRadioButtonId());
                    Toast.makeText(MainActivity.this,""+rb.getText().toString(),Toast.LENGTH_SHORT).show();
            }
        });
        }
        
        
        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        
        <RadioGroup
            android:id="@+id/rad_grp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        
            <RadioButton
                android:id="@+id/male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male"/>
            <RadioButton
                android:id="@+id/female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"/>
            <RadioButton
                android:id="@+id/other"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Other"/>
        
        
        </RadioGroup>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp">
        
            <Button
                android:id="@+id/clear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Clear"
                android:layout_marginLeft="20dp"/>
        
            <Button
                android:id="@+id/submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Submit"
                android:layout_marginLeft="20dp"/>
        
        </LinearLayout>
        
        
        
        </LinearLayout>
        

        【讨论】:

        • 您的答案只是代码。请在您的代码中添加一些解释以使其有用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-22
        • 2011-07-19
        • 1970-01-01
        • 2011-09-02
        • 1970-01-01
        相关资源
        最近更新 更多