Android中RadioGroup的初始化和简单的使用

一简介:

RadioGroup作为一个单选按钮组,可以设置为性别选择男或则女,地址选择等等,作为一个android入门级选手,就简单的说一下RadioGroup组中RadioButton的布局和初始化操作,以及禁用整个RadioGroup。

二具体介绍:

布局:

  <RadioGroup
                        android:id="@+id/rg_Orientation"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="10dp">

                        <RadioButton
                            android:id="@+id/rb_Portrait"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:checked="true"
                            android:drawableRight="@drawable/r_portrait" />

                        <RadioButton
                            android:id="@+id/rb_Landscape"
                           android:layout_marginTop="10dp"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:drawableRight="@drawable/r_landscape"/>
                    RadioGroup>

初始化:

 radioGroup_orientation = (RadioGroup) findViewById(R.id.rg_Orientation);

给初始化完成的RadioGroup设置监听

radioGroup_orientation.setOnCheckedChangeListener(radioGrouplisten);

监听的具体逻辑

//RadioGroup控件的初始化
    private RadioGroup.OnCheckedChangeListener radioGrouplisten = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int id = group.getCheckedRadioButtonId();
            switch (group.getCheckedRadioButtonId()) {
                case R.id.rb_Landscape:
                    orientation = Orientation.landscape;
                    Log.i("orientation",orientation.toString());
                    //Toast.makeText(PrintSettingActivity.this, orientation.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case R.id.rb_Portrait:
                    orientation = Orientation.Portrait;
                    Log.i("orientation",orientation.toString());
                    //Toast.makeText(PrintSettingActivity.this, orientation.toString(), Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

RadioGroup的所有单选按钮均不可点击,需要遍历RadioGroup中的每一个单选按钮,可以根据限定条件来对按钮进行控制

 public void disableRadioGroup(RadioGroup radioGroup,String fileName){
        if(recPath.endsWith("pdf")){
            for (int i = 0; i < radioGroup.getChildCount(); i++) {
                radioGroup.getChildAt(i).setEnabled(false);
            }
        }
    }

三总结:

  

  

 

转载于:https://www.cnblogs.com/mlgm/p/9673277.html

你可能感兴趣的:(移动开发,java)