【问题标题】:When I select the radio button , it should submit the answer and open next activity in the same time. How can I do that?当我选择单选按钮时,它应该同时提交答案并打开下一个活动。我怎样才能做到这一点?
【发布时间】:2017-05-26 14:31:12
【问题描述】:

我正在尝试使用多项选择进行简单的测验应用程序。当我选择单选按钮时,单选按钮应提交答案并打开下一个活动。在我的代码中有一个“提交”按钮。所以首先选择单选按钮,然后单击提交按钮并拖动页面以打开另一个页面,但这没有用。我想做所有这些,只需选择单选按钮。

 public class Question1 extends Fragment {
     RadioButton q1a2;
     Button btn1;

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
         View v = inflater.inflate(R.layout.fragment_question1, null);
         return v;
     }

     public void onActivityCreated(Bundle savedInstanceState){
         super.onActivityCreated(savedInstanceState);
         q1a2 = (RadioButton)getView().findViewById(R.id.q1a2);
         btn1 =(Button)getView().findViewById(R.id.btn1);

         final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
         btn1.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {
                 SharedPreferences.Editor editor = app_preferences.edit();
                 if(q1a2.isChecked()){
                     editor.putInt("answer_value", 1);
                 }else{
                     editor.putInt("answer_value", 0);
                 }

                 editor.commit();
             }
         });
      }
  }

【问题讨论】:

  • 你的问题到底是什么?

标签: android radio-button android-sharedpreferences


【解决方案1】:

不要使用不同的单选按钮侦听器和提交按钮来显式获取单选按钮点击,而是使用带有单选按钮的 radiogroup。然后使用 RadioGroup.OnCheckedChangeListener 获取单击的单选按钮的 id 并以同样的方法开始新的活动。

示例代码如下:

RadioGroup radio = (RadioGroup) dialog.findViewById(R.id.radioGroup1);
        radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                View radioButton = radio.findViewById(checkedId);
                int index = radio.indexOfChild(radioButton);

                switch (index) {
                case 0: // first button
                        break;
                case 1: // second button
                        break;
                    case 3: // third button
                        break;
                    case 4: // fourth button
                        break;
                }
            }
        }); 

【讨论】:

    【解决方案2】:

    在您的视图 xml 中,您必须拥有 RadioButton 所在的 RadioGroup。

    RadioGroup yourRadioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
    yourRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // Here you can find selected radio button Id.
            // On the basis of Radio Button you can decide it is selected or not
    
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多