Android 暑期巩固与进阶——控件篇

1 常用控件

1.1 Textview

1.1.1 定义控件

<TextView
        android:layout_width=""
        android:layout_height=""
        />

1.1.2 常用属性

<!--代码不全-->
<TextView
         android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="在这个属性输入内容"
        android:inputType="textEmailAddress"
        android:gravity="center"
        android:background="@color/design_default_color_on_secondary"
        android:textColor="#FFFFFF"
        android:textSize="20dp"
        android:textStyle="italic"/>

*一般来如果需要其他属性的功能的话,上网直接搜

1.2 EidtText

1.2.1 定义控件

<EditText
        android:layout_width=""
        android:layout_height=""/>

1.2.2 常用属性

<EditText
        android:hint="输入提示词"
        android:textColorHint="@color/white"
        android:maxLength="@android:integer/config_longAnimTime"
        android:inputType="textEmailAddress"
        android:background="@color/black"/>

1.2.3 自定义

示例:制作一个自定义边框,当焦点选中边框,icon变色的edittext

(1)在drawable文件夹里面创建一个名为custom_input的选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <corners android:radius="10dp"/>
            <stroke android:color="#5D88AB" android:width="2dp"/>
        </shape>
    </item>
​
    <item android:state_enabled="true" >
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <corners android:radius="10dp"/>
            <stroke android:color="#6F6D6D" android:width="1dp"/>
        </shape>
    </item>
</selector>

你的icon也要做类似的工作,焦点选中时icon也变色,创建一个名为的资源文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
        android:drawable="@drawable/ic_email_focused"/>
​
    <item android:state_focused="false"
        android:drawable="@drawable/ic_email"/>
​
</selector>

(2)在xml文件里面去调用

<EditText
            android:id="@+id/edt_pwd"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="@drawable/custom_input"
            android:drawableStart="@drawable/custom_lock_icon"
            android:drawablePadding="11dp"
            android:ems="15"
            android:hint="登录密码"
            android:inputType="textEmailAddress"
            android:paddingStart="12dp"
            android:paddingEnd="12dp"
            android:textSize="15sp" />

效果:

 

*这里的色调有点问题,其实边框是变了色的,可以选点亮色比如紫色效果更明显

1.3 Imageview

1.3.1 定义控件

<ImageView
        android:layout_width=""
        android:layout_height=""/>

1.3.2 常用属性

 <ImageView
        android:maxWidth="200dp"
        android:maxHeight="200dp"
        android:src="@drawable/ic_launcher_foreground"/>

1.3.3 好用的库(这个会一直更新,大概)

1.3.3.1.Glide库

推荐博客:(42条消息) Glide介绍及基本使用方法glide使用weixin_49078536的博客-CSDN博客

(1)官方介绍

Glide是一个快速高效的Android图片加载库,注重于平滑的滚动。Glide提供了易用的API,高性能、可扩展的图片解码管道(decode pipeline),以及自动的资源池技术。Glide 支持拉取,解码和展示视频快照,图片,和GIF动画。Glide的Api是如此的灵活,开发者甚至可以插入和替换成自己喜爱的任何网络栈。默认情况下,Glide使用的是一个定制化的基于HttpUrlConnection的栈,但同时也提供了与Google Volley和Square OkHttp快速集成的工具库。虽然Glide 的主要目标是让任何形式的图片列表的滚动尽可能地变得更快、更平滑,但实际上,Glide几乎能满足你对远程图片的拉取/缩放/显示的一切需求。

(2)基本用法

Glide 建造要求最少有三个参数。

Glide.with(context)     //可以传入fragment 、activity
    .load(url)      //图片资源。可以是resourceId 、url 、uri 、drawable 、file 等
    .into(imageView);       //图片会显示到对应的 ImageView 中

(3)示例使用

首先是添加依赖

implementation 'com.github.bumptech.glide:glide:4.12.0'

*如果需要调用传回来的url之类的,用到网络请求,还需要在manifests添加一定的权限

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

然后在xml文件里面放一个imageview

<ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

xml布局的样子是这样的

修改java代码

package com.gxy.text1;
​
import androidx.appcompat.app.AppCompatActivity;
​
import android.os.Bundle;
import android.widget.ImageView;
​
import com.bumptech.glide.Glide;
​
public class MainActivity extends AppCompatActivity {
​
    private ImageView imageView;
​
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.img);
        Glide.with(this)
                .load("https://lmg.jj20.com/up/allimg/tp09/210H51R3313N3-0-lp.jpg")
                .into(imageView);
    }
}

效果如下:

有一些常用的功能,很方便实用

#将传来的图片以圆形加载

//两种方法,选择其中一个
.apply(RequestOptions.circleCropTransform())
.circleCrop()

#缩略图

.thumbnail(0.1f)    //你传了一个 0.1f 作为参数,Glide 将会显示原始图像的10%的大小

#压缩或拉伸

.override(1000,1000)

加载GIF

本地,将gif图存在drawable文件夹里面

示例:

package com.gxy.text1;
​
import androidx.appcompat.app.AppCompatActivity;
​
import android.os.Bundle;
import android.widget.ImageView;
​
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
​
public class MainActivity extends AppCompatActivity {
​
    private ImageView imageView;
​
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.img);
        Glide.with(this)
                .load(R.drawable.eli)
                //.apply(RequestOptions.circleCropTransform())
                .circleCrop()
                .into(imageView);
    }
}

效果

1.4 Button

1.4.1 定义控件

<Button
    android:layout_width=""
    android:layout_height=""/>

1.4.2 常用属性

<Button
        android:background="#90A5E3"
        android:text="不要点击我"/>

*注意这里修改颜色时,需要将values文件里面的themes,后面多加 ".bridge"

自主学习的时候一般的属性就够用了,如果想要学习更多的属性网上找便可,自定义button也可以网上找

1.4.3 给按钮设置监听触发点击事件

1.4.3.1 onclick

首先在控件添加onclick属性

<Button
        android:onClick="click"/>

注意等号后面的东西就是那后面在java文件里面创建的函数的名称

示例:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
​
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.212" />
​
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:textColor="@color/white"
        android:textSize="50dp"
        android:text="Hello World"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
​
</androidx.constraintlayout.widget.ConstraintLayout>

然后修改java代码

package com.gxy.buttontext;
​
import androidx.appcompat.app.AppCompatActivity;
​
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.TextView;
​
public class MainActivity extends AppCompatActivity {
​
    private TextView text;
    private Button button;
    private ObjectAnimator animator1,animator2;
    private int cnt = 1;
​
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.tv);
        button = findViewById(R.id.btn);
        animator1=ObjectAnimator.ofFloat(text,"rotation",0f,60f);
        animator1.setDuration(1000);
        animator1.setInterpolator(new LinearInterpolator());
        animator1.setRepeatCount(0); //转动重复次数为0
        animator2=ObjectAnimator.ofFloat(text,"rotation",60f,0f);
        animator2.setDuration(1000);
        animator2.setInterpolator(new LinearInterpolator());
        animator2.setRepeatCount(0);
​
    }
​
    public void click(View view){
        if(cnt == 1){
            animator1.start();
            cnt--;
        }else{
            animator2.start();
            cnt++;
        }
    }
}

效果,点击一次旋转,再点击一次逆旋转回归原位

1.4.3.2 设置监听

在java文件里面需要先绑定控件,如下:

private Button button;  //建议全局定义对象
//OnCreate方法里面绑定
button = findViewById(R.id._____);

然后设置监听,如下:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });

*之前的控件也能设置监听

示例:(也是刚刚的旋转)

将xml的onclick属性删除,修改Java文件

//......
​
public class MainActivity extends AppCompatActivity {
    //......
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.tv);
        button = findViewById(R.id.btn);
        /*动画省略*/
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cnt == 1){
                    animator1.start();
                    cnt--;
                }else{
                    animator2.start();
                    cnt++;
                }
            }
        });
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值