Android开发-RadioButton,CheckBox,ImageView

RadioButton

  • 常用属性
  • 自定义样式
  • 监听事件

选择效果图

RadioButton

代码

activity_radio_button.xml文件



    

        

        
    

    

        

        

    


选项背景效果
selector_orange_radiobtn.xml




    
        
            
            
        
    

    
        
            
            
        
    


RadioButtonActivity代码

public class RadioButtonActivity extends AppCompatActivity {

    private RadioGroup mRG1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);

        mRG1 = findViewById(R.id.rg_1);
        mRG1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = group.findViewById(checkedId);
                Toast.makeText(RadioButtonActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

CheckBox

  • 常用属性
  • 自定义样式
  • 监听事件

复选框效果图


复选框效果图

代码
activity_check_box.xml文件



    

    

    

    

    

    
        

        
        
    


CheckBoxActivity

public class CheckBoxActivity extends AppCompatActivity {

    private CheckBox mCb5, mCb6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        mCb5 = findViewById(R.id.cb_5);
        mCb6 = findViewById(R.id.cb_6);

        mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this, isChecked ? "5选中" : "5未选中", Toast.LENGTH_SHORT).show();
            }
        });

        mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this, isChecked ? "6选中" : "6未选中", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

ImageView

  • 常用属性
  • 加载网络图片

scaleType

  • fitXY: 撑满控件,宽高比可能发生改变
  • fitCenter: 保持宽高比缩放,直至完全显示
  • centerCrop: 保持宽高比缩放,直至完全覆盖控件,裁剪显示

效果图

ImageView效果图

代码

activity_image_view.xml



    

    

    

    

ImageViewActivity文件

public class ImageViewActivity extends AppCompatActivity {

    private ImageView mIv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);

        mIv4 = findViewById(R.id.iv_4);
        Glide.with(this).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603459341312&di=bc520d0ed58b8e3786e74ef3f8442985&imgtype=0&src=http%3A%2F%2Fz1.dfcfw.com%2F2020%2F10%2F14%2F20201014075848343834694.jpg").into(mIv4);
    }
}

添加第三方库

repositories {
    google()
    jcenter()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:2.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}

使用Sync Now后自动导入图片加载库

你可能感兴趣的:(Android开发-RadioButton,CheckBox,ImageView)