分享好友 维修大全首页 维修大全分类 切换频道

吉日象维修app

switchermod怎么用(ImageView、Switch以及DatePicker的使用)

2023-11-01760

一、各自的功能及使用场景

ImageView:用于展示一张图片,例如商品展示;

Switch:开关、控制器,常用于app设置中;

DatePicker:日期选择器,多用于订阅类的服务和功能

二、简单使用

1、布局部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <Button
        android:id="@+id/btn_todo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLICK"/>
    <Switch
        android:id="@+id/s_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"/>
    <TextView
        android:id="@+id/tv_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <DatePicker
        android:id="@+id/dp_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

2、代码部分

  private TextView tvDate;
    private Button btnTodo;
    private Switch sToggle;
    private DatePicker dpDate;
    private ImageView ivImg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        ivImg.setImageResource(R.drawable.ic_launcher_foreground);
    }

    private void setListener() {
       btnTodo.setonClickListener(v -> {
           Toast.makeText(this, "按钮被点击", Toast.LENGTH_SHORT).show();
       });
       sToggle.setonCheckedChangeListener(new CompoundButton.onCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               btnTodo.setEnabled(isChecked);
           }
       });

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            dpDate.setonDateChangedListener(new DatePicker.onDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    tvDate.setText(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
                }
            });
        }
    }

    private void initView() {
        tvDate=findViewById(R.id.tv_date);
        btnTodo=findViewById(R.id.btn_todo);
        sToggle=findViewById(R.id.s_toggle);
        dpDate=findViewById(R.id.dp_date);
        ivImg=findViewById(R.id.iv_img);
    }
}

代码中,我通过sToggle来控制btnTodo是否可点击,将dpDate获取到的日期用tvDate展示,之所以会+1是因为月份获取到的数字是从0开始的

三、运行效果


四、官网对于上述控件的属性介绍

1、ImageView

android:adjustViewBounds

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

android:baseline

The offset of the baseline within this view.

android:baselineAlignBottom

If true, the image view will be baseline aligned with based on its bottom edge.

android:cropToPadding

If true, the image will be cropped to fit within its padding.

android:maxHeight

An optional argument to supply a maximum height for this view.

android:maxWidth

An optional argument to supply a maximum width for this view.

android:scaleType

Controls how the image should be resized or moved to match the size of this ImageView.

android:src

Sets a drawable as the content of this ImageView.

android:tint

The tinting color for the image.

android:tintMode

Blending mode used to apply the image tint.

2、Switch

android:showText

Whether to draw on/off text.

android:splitTrack

Whether to split the track and leave a gap for the thumb drawable.

android:switchMinWidth

Minimum width for the switch component.

android:switchPadding

Minimum space between the switch and caption text.

android:switchTextAppearance

TextAppearance style for text displayed on the switch thumb.

android:textOff

Text to use when the switch is in the unchecked/"off" state.

android:textOn

Text to use when the switch is in the checked/"on" state.

android:textStyle

Style (normal, bold, italic, bold|italic) for the text.

android:thumb

Drawable to use as the "thumb" that switches back and forth.

android:thumbTextPadding

Amount of padding on either side of text within the switch thumb.

android:thumbTint

Tint to apply to the thumb.

android:thumbTintMode

Blending mode used to apply the thumb tint.

android:track

Drawable to use as the "track" that the switch thumb slides within.

android:trackTint

Tint to apply to the track.

android:trackTintMode

Blending mode used to apply the track tint.

android:typeface

Typeface (normal, sans, serif, monospace) for the text.

3、DatePicker


android:calendarTextColor

The text color list of the calendar.

android:calendarViewShown

Whether the calendar view is shown.

android:datePickerMode

Defines the look of the widget.

android:dayOfWeekBackground

The background color for the header's day of week.

android:dayOfWeekTextAppearance

The text color for the header's day of week.

android:endYear

The last year (inclusive), for example "2010".

android:firstDayOfWeek

The first day of week according to Calendar.

android:headerBackground

The background for the selected date header.

android:headerDayOfMonthTextAppearance

The text appearance for the day of month (ex.

android:headerMonthTextAppearance

The text appearance for the month (ex.

android:headerYearTextAppearance

The text appearance for the year (ex.

android:maxDate

The maximal date shown by this calendar view in mm/dd/yyyy format.

android:minDate

The minimal date shown by this calendar view in mm/dd/yyyy format.

android:spinnersShown

Whether the spinners are shown.

android:startYear

The first year (inclusive), for example "1940".

android:yearListItemTextAppearance

The list year's text appearance in the list.

android:yearListSelectorColor

The list year's selected circle color in the list.

五、补充

一般来讲,有什么属性,就有对应的set和get方法;另外对代码中的if语句块作个简单说明:它表示android版本大于等于8.0以上才执行if里的内容

举报
打赏
打赏主播是什么意思
打赏主播是指观众(用户)在观看网络直播节目时,自愿通过直播平台提供的支付渠道,将一定数额的金钱或虚拟礼物赠送给主播的行为

0评论2025-03-26870

主打是什么意思
“主打” 这个词常见的有以下几种意思:一、在商业、产品领域主要营销、重点推广当我们说某产品是公司的 “主打产品” 时,是指

0评论2025-03-26636

电台路是什么意思
“电台路” 通常是因与电台相关的因素而得名的道路,以下是一些具体例子:上海电台路:位于宝山区顾村镇,呈南北走向,南起宝安

0评论2025-03-26335

打狙的窍门是什么意思
“打狙的窍门” 通常是指在射击游戏或实际射击场景中,使用狙击步枪时能够提高射击准确性、效率和生存能力等的一些技巧和方法。

0评论2025-03-26647

路上的创作原声是什么意思
“路上的创作原声” 通常是指以 “在路上” 的状态、经历、感悟等为主题或灵感来源而创作的原始声音作品,在不同艺术领域有不同

0评论2025-03-26375

打印照片回执是什么意思
打印照片回执是指在完成照片拍摄并经相关系统审核通过后,将记录照片合格信息以及个人身份等相关内容的电子凭证,通过打印机输出

0评论2025-03-26283

棒打鸳鸯是什么意思
“棒打鸳鸯” 是一个汉语成语,意思是用木棒打散一对鸳鸯,比喻拆散恩爱的夫妻或情侣。该成语的来源和用法如下:来源与出处:出

0评论2025-03-26672

主打三棺是什么意思
“主打三棺” 是一种网络用语,用反语的方式讽刺人们没有正常的思维观念和底线。该梗起源于贴吧网友之间的讽刺话语,具体出自抗

0评论2025-03-26220

常用香料是什么意思
常用香料是指在烹饪、食品加工、香水制作、药品生产等领域中经常使用的具有独特香气和味道的物质。它们可以增添风味、改善气味、

0评论2025-03-26963

潮汕话香芋是什么意思
在潮汕话中,“香芋” 通常指的就是芋头。不过,潮汕方言中芋头的发音是 “麦筲”(mài shāo)。芋头在潮汕地区的饮食文化和民

0评论2025-03-26504

关于我们  |  网站留言
(c)2026 吉日象维修app www.mykuaidi.com