Android EditText基于radioGroup选择的可见性

时间:2015-02-08 10:11:59

标签: android visibility

如果选择了editText1,我需要隐藏radioButton1并显示editText2 如果选择了editText2,我需要隐藏radioButton2并显示editText1。 两个按钮都在radioGroup中。 我不确定是否有一个radioGroup onChange事件返回哪个radioButton被检查? 如果是的话,我可以做到

EditText et1 = (EditText) findViewById(R.id.editText1);
et1.setVisibility(View.INVISIBLE);

3 个答案:

答案 0 :(得分:2)

您可以将OnCheckedChangeListener()设置为您的radioGroup:

      final EditText et1 = (EditText) findViewById(R.id.editText1);
      final EditText et2 = (EditText) findViewById(R.id.editText2);

      RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);        
      radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId == R.id.radioBtn1){
                       et1.setVisibility(View.VISIBLE);
                       et2.setVisibility(View.INVISIBLE);  
                    } else {
                       et1.setVisibility(View.INVISIBLE);
                       et2.setVisibility(View.VISIBLE);  
                    }
                }
       });

答案 1 :(得分:0)

您需要在OnCheckedChangeListener上添加radioGroup

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {         
        // checkedId is the RadioButton selected
        if (checkedId == R.id.radioButton1) {
            editText1.setVisibility(View.INVISIBLE);
            editText2.setVisibility(View.VISIBLE);
        } else if (checkedId == R.id.radioButton1) {
            editText1.setVisibility(View.VISIBLE);
            editText2.setVisibility(View.INVISIBLE);
        }
    }
});

答案 2 :(得分:0)

您可以通过Rami和sv3k解决您的功能。

还有另一种选择,你可以尝试达到你想要的。 这是在每个单选按钮上设置onClickListener。

喜欢这个。

   RadioGroup  radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
        RadioButton radio1= (RadioButton)radioGroup.findViewById(R.id.radio1);
        RadioButton radio2= (RadioButton)radioGroup.findViewById(R.id.radio2);
        radio1.setOnClickListener(new View.OnClickListener() 
         {
             @Override
             public void onClick(View v) {
                 // TODO Auto-generated method stub
                 et1.setVisibility(View.INVISIBLE);
                 et2.setVisibility(View.VISIBLE);
             }
         });

        radio2.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                et2.setVisibility(View.INVISIBLE);
                et1.setVisibility(View.VISIBLE);
            }
        });