使用单选按钮更改微调器的可见性

Change visibility of spinner with radio button

我想在更改单选按钮时更改微调器的可见性。如果我这样做,两个微调器都是 View.GONE,当我单击其中一个单选按钮时,没有任何反应,微调器保持 View.GONE,而不是 View.VISIBLE。我试图将 IF 放入 onViewCreated,但结果是一样的。我将不胜感激任何帮助。

RadioButton rb1;
RadioButton rb2;
Spinner spinner2;
Spinner spinner1;
ArrayAdapter<CharSequence> adapter;
ArrayAdapter<CharSequence> adapter2;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {       

        View view = inflater.inflate(R.layout.fragment_floating_button, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 

        rb1 = (RadioButton) view.findViewById(R.id.rb1);
        rb2 = (RadioButton) view.findViewById(R.id.rb2);

        spinner2 = (Spinner) view.findViewById(R.id.spinner2);
        spinner1 = (Spinner) view.findViewById(R.id.spinner1);

        if(rb1.isChecked()){
            spinner1.setVisibility(View.VISIBLE);
            spinner1.setClickable(true);
        }
        else {spinner1.setVisibility(View.GONE);
            spinner1.setClickable(false);}
        if (rb2.isChecked()){
            spinner2.setVisibility(View.VISIBLE);
            spinner2.setClickable(true);
        }
        else {
            spinner2.setVisibility(View.GONE);
            spinner2.setClickable(false);
        }
        return view;
    }



public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        adapter= ArrayAdapter.createFromResource(getActivity(),R.array.array2,android.R.layout.simple_spinner_item);
        adapter2= ArrayAdapter.createFromResource(getActivity(),R.array.array1,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);         adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner2.setAdapter(adapter);
        spinner1.setAdapter(adapter2);

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
                                              @Override
                                              public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                                                  switch (position) {
                                                      case 0:
                                                          ....

                                                      default:

                                                          break;
                                                  }
                                              }

                                              @Override
                                              public void onNothingSelected(AdapterView<?> parent) {

                                              }
                                          }
        );

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
                                                    @Override
                                                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                                                        switch (position) {
                                                            case 0:
                                               ....
                                                                break;

                                                            default:

                                                                break;
                                                        }
                                                    }

                                                    @Override
                                                    public void onNothingSelected(AdapterView<?> parent) {
                                                    }
                                                }
        );
    }

您必须更改 RadioButton 侦听器中的可见性,因此每次单选按钮更改时微调器都会更新。

rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                spinner1.setVisibility(View.VISIBLE);
                spinner1.setClickable(true);
            }
            else {spinner1.setVisibility(View.GONE);
                spinner1.setClickable(false);}
        }
    });

你的问题是你的代码将 运行 一次然后停止,如果无线电 1 被检查它会做一些事情,如果无线电 2 被检查它会做一些事情然后停止。

您应该为两个单选按钮添加点击侦听器,然后像那样粘贴您的代码

//click first radio button

rb1.setonClickListener(new OnClickListener{
 //your code

   if(rb1.isChecked()){
        spinner1.setVisibility(View.VISIBLE);
        spinner1.setClickable(true);
    }
    else {spinner1.setVisibility(View.GONE);
        spinner1.setClickable(false);}



});

 //second click radio button 2

  rb2.setonClickListener(new OnClickListener{

   //your code here
     if (rb2.isChecked()){
        spinner2.setVisibility(View.VISIBLE);
        spinner2.setClickable(true);
    }
    else {
        spinner2.setVisibility(View.GONE);
        spinner2.setClickable(false);
    }


  });