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

时间:2017-11-04 17:32:10

标签: java android android-fragments android-spinner android-radiobutton

我希望在更改单选按钮时更改微调器的可见性。如果我这样做,两个微调器都是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) {
                                                    }
                                                }
        );
    }

2 个答案:

答案 0 :(得分:1)

您必须更改RadioButton侦听器内的可见性,因此每次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 :(得分:0)

你的问题是你的代码会运行一次然后停止,如果无线电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);
    }


  });