Android 学习笔记(3)—— ImageView/RadioButton/CheckBox

本文深入解析Android开发中ImageView、RadioButton与CheckBox的使用方法,通过实例演示了如何利用这些控件实现图像显示、单选按钮与复选框的功能。包括属性设置、布局实现以及交互事件处理等关键步骤,旨在帮助开发者掌握这些基本控件的高效应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者:夏至 欢迎转载,也请保留这段申明,谢谢

1. ImageView

ImageView 呢,是图像按钮,我们可以用它来显示我们想要显示的图像。 其中最主要的属性就是 android:scaleType = ? 这里有几个常用属性:
1. center 就是按照一定的比例来裁剪缩放
2. fitCenter 就是按照我们设定好的内容缩放,默认是采用这个模式
3. fitXY 使图片充满这个容器
一般我们都是用着三个属性,如果你想了解更多,可以去查API。这里我们就简单用一下第一个和第二个属性,第三个可以自行验证。

我们先定义两个 ImageView 控件来显示不一样的效果

<ImageView
        android:layout_width="75dp"
        android:layout_height="64dp"
        android:scaleType="fitCenter"
        android:src="@drawable/image5"/>
<ImageView
        android:id="@+id/imageDemo"
        android:layout_width="75dp"
        android:layout_height="64dp"
         android:scaleType="center"
        android:src="@drawable/image5"/>

效果如图:
这里写图片描述
可以看到第一个图片按照我们的压缩比例缩放,第二张则是裁剪了中间部分。当然你也可以在activity 实现

当我们使用center属性时,我们可以用主activity来实现裁剪

public class ImageViewDemo extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageview);
        ImageView imageView = (ImageView)findViewById(R.id.imageDemo);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(300,300));
    }
}

效果如下:
这里写图片描述

2. RadioButton (单击按钮)

这里写图片描述

如题所示,单击按钮只能选中一次,如上图所示,我们在做调查问卷的时候,经常用到。而这个功能的实现呢,我们用radiogroup和radiobutton实现。把radiobutton内层放到radiogroup里,然后设置好外层的对齐方式即可。外层RadioGroup设置orientation属性然后设置RadioButton的排列方式,是竖直还是水平~。布局文件如下:

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="使用单击按钮"
    android:textSize="24sp"
/>
<RadioGroup
    android:id="@+id/group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男"
        android:checked="true"
    />
 <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女"
        android:checked="true"
    />

ok,layout层的程序我们已经写好,那么,当我们选择的时候,怎么提示呢?我们用toast来显示里我们可以在主函数中来判断。如:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId); //这里用checkedId,来遍历
Toast.makeText(MainActivity.this, "按钮组值发生改变,你选了" +
                radioButton.getText(), Toast.LENGTH_SHORT).show();

这里写图片描述

但一般我们不这样来显示,我们一般用一个按键来对选择的内容进行提示,所以这里,我们还要在layout层添加一个button组件:

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="确定"
    android:textSize="24sp"
/>

在主activity那里,我们要写button的事件函数:

    ......
    radioGroup = (RadioGroup) findViewById(R.id.group);
    Button button = (Button)findViewById(R.id.btn);
    button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    // radioGroup.getChildCount()获得按钮组中的单选按钮的数目;
    for (int i = 0;i<radioGroup.getChildCount();i++){
    //radioGroup.getChildAt(i) 根据索引值获取我们的单选按钮
    RadioButton rad = (RadioButton)radioGroup.getChildAt(i);
        if(rad.isChecked()){  // isChecked()判断按钮是否选中
            Toast.makeText(MainActivity.this,"你选择的性别是: "+rad.getText(),
            Toast.LENGTH_SHORT).show();
            break;

效果如图:
这里写图片描述

3.CheckBox(复选框)

如题所示,这个是我们的复选框,那么我们在多项选择的时候,是经常性地应用到。那么它究竟张什么样呢
这里写图片描述

没错,那么我们现在就在layout上添加我们的程序吧

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textScaleX="24sp"
    android:text="请选择你喜欢的水果"
/>
<CheckBox
    android:id="@+id/one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="香蕉"
/>
<CheckBox
    android:id="@+id/two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="苹果"
/>
<CheckBox
    android:id="@+id/three"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="菠萝"
/>
<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="提交"
/>

在调用上,我们采用第二个单选的方法,让一个button来确定选中了多少个:

....
button = (Button)findViewById(R.id.btn);
one = (CheckBox)findViewById(R.id.one);
two = (CheckBox)findViewById(R.id.two);
three = (CheckBox)findViewById(R.id.three);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String choose = "";
    if(one.isChecked())
        choose += one.getText().toString();
    if(two.isChecked())
        choose += two.getText().toString();
    if(three.isChecked())
        choose += three.getText().toString();
Toast.makeText(MainActivity.this,choose,Toast.LENGTH_SHORT).show();
}

这里写图片描述

如有错误,欢迎指出,如果喜欢,欢迎收藏!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值