Android试图获取RadioGroup对象,以便我可以隐藏它

时间:2018-03-15 11:19:03

标签: java android

尝试获取对xml文件中定义的无线电组的引用。 通常我使用findViewById(R.id.idAll);来获取对ui对象的引用。无法弄清楚如何为RadioGroup执行此操作。

2 个答案:

答案 0 :(得分:0)

广播组与任何视图相同。你可以使用

 RadioGroup rg = findViewById(R.id.idAll);

答案 1 :(得分:0)

试试这个

  rg = (RadioGroup) findViewById(R.id.radioGroup1);

        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

            // Method 1 For Getting Index of RadioButton
            pos=rg.indexOfChild(findViewById(checkedId));

                Toast.makeText(getBaseContext(), "Method 1 ID = "+String.valueOf(pos),
                Toast.LENGTH_SHORT).show();

                //Method 2 For Getting Index of RadioButton
            pos1=rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));

                Toast.makeText(getBaseContext(), "Method 2 ID = "+String.valueOf(pos1),
                Toast.LENGTH_SHORT).show();

                switch (pos)
                {
                case 0 :
                Toast.makeText(getBaseContext(), "You have Clicked RadioButton 1",
                    Toast.LENGTH_SHORT).show();
                    break;
                case 1 :
                    Toast.makeText(getBaseContext(), "You have Clicked RadioButton 2",
                    Toast.LENGTH_SHORT).show();
                    break;
                case 2 :
                    Toast.makeText(getBaseContext(), "You have Clicked RadioButton 3",
                    Toast.LENGTH_SHORT).show();
                    break;
            default :
                //The default selection is RadioButton 1
                    Toast.makeText(getBaseContext(),"You have Clicked RadioButton 1" ,
                    Toast.LENGTH_SHORT).show();
                    break;
                }
        }
    });