[基础01]基础控件(上)-TextView-Button-EditText-ImageView

一、TextView

1.普通的TextVie

<TextView
	android:id="@+id/tv_this_text" id便于java代码中查找该控件
	android:layout_width="match_parent"  匹配父控件大小
	android:layout_height="wrap_content"  匹配内容大小
	android:text="这是一个TextView"
	android:textSize="20sp"
	android:gravity="center"  layout_gravity定义的是控件在父布局中的相对位置
	android:textStyle="bold"  加粗显示
	android:background="#80A3D15F"
/>	

2.带阴影的TextView

单独设置其中一个参数是没有效果的,这几个参数合起来才能看出对应的效果

<TextView
        android:id="@+id/tv_shadow_text"
        ...
        android:text="This is a shadow text view"
        ...
        
        android:shadowColor="#E4E3365C"   阴影颜色
        android:shadowDx="10.0"  阴影x y偏移量
        android:shadowDy="10.0"
        android:shadowRadius="3.0"  阴影模糊效果
        />

3.跑马丁效果的TextView

<TextView
        ...  text长度要大于控件长度
        android:text="This is a running text view, single line can be runnable!"
        ...
        android:maxLines="1" 最大行数
        android:ellipsize="marquee" 跑马灯效果,end start middle都是省略号展示方式
        android:marqueeRepeatLimit="marquee_forever" 循环次数无限次
        android:focusable="true" 获取焦点
        android:focusableInTouchMode="true"
        android:clickable="true" 是否可点击
        />

上边的方法需要点击一次文字才会滚动,要实现自动滚动,可以用下边的方法
方法1: 自定义view重写isFocused()方法,直接返回true

//继承自TextView,实现构造器
@Override
public boolean isFocused() {
    return true;
}

方法2: 在布局文件中申请焦点

<TextView
	...
	...>
	<requestFocus/>
/>

二、Button

1.普通的Button控件

<Button
   android:layout_width="200dp"     宽高属性
    android:layout_height="100dp"
    android:text="Button 1"    Button文字属性
    android:textSize="25sp"    文字大小
    android:background="#66558833"    背景颜色
    />

2.状态选择器

2.1 StateListDrawable状态选择器

res/drawable文件中新建一个状态选择器,在根据需要定义不同的<item/>属性

//注意这里是新建在res/drawable文件中的btn_select.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	//按下时显示这个drawable
    <item android:drawable="@drawable/btn_select_drawable" android:state_pressed="true"/>
    //松开默认时显示这个drawable
    <item android:drawable="@drawable/btn_default_drawable" android:state_pressed="false"/>
</selector>

//布局中的Button控价
<Button
   ...宽高、文字等配置
   android:background="@drawable/btn_select"  直接引用选择器资源即可
   />

2.2 Button的颜色选择器

与状态选择器类似颜色选择器也是让Button控件访问drawable中的资源
首先在res文件夹中新建color文件夹,然后在res/color目录下新建颜色选择器

//res/drawable/btn_color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	//选中与未选中的颜色
    <item android:color="#66ee3344" android:state_pressed="true"/>
    <item android:color="@color/purple_700" android:state_pressed="false"/>
</selector>

<Button
  ...宽高等属性
   android:background="@drawable/btn_select"
   android:backgroundTint="@color/btn_color_selector"  直接引用布局文件
   />

补充:

  • 这里也可以设置前景色foregroundTint,以下是三者的关系
  • 还有其他的状态属性用表格列出
名称作用/解释
drawable引用的Drawable位图
state_focused是否获得焦点
state_pressed控价是否被按下
state_enabled控件是否可用
state_selected控件是否被选择,针对有滚轮的情况
state_checked控件是否被勾选
state_checkable控件是否可以被勾选
state_window_focused是否获得窗口焦点
state_active控件是否处于活动状态 eg: slidingTab
state_single包含多个子控件时,确定是否只选择一个子控件
state_first包含多个子控件时,确定第一个子控件是否为显示状态
state_middle多个子控件时,确定中间一个子控件处于显示状态
state_last多个子控件,确定最后一个处于显示状态

3.Button的事件处理

  • 点击事件的传递顺序onTouch()-->onLonclick()-->onClick()
    如果前两个的返回值为true即事件被消费,那么事件就不会传递到后边的点击事件当中

3.1 点击事件onClick()

  • 点击事件有两种实现方法
    一种是通过setOnClickListener()方法来定义点击事件
    另一种是在xml中添加对应的click标签之后在对应的activity中实现对应click标签的方法来实现
Button buttonClick = findViewById(R.id.btn_click_event);
//方法1:通过setonClickListner()方法实现点击
buttonClick.setOnClickListener(view -> {
    //点击事件
    Log.e(TAG, "onClick");
    /**事件传递顺序onTouch->onLongClick->onClick
        如果前边的任意一个事件被消费,那都不会走到onClick里边来
     */
});
//方法2:
//先在xml中添加对应的标签
/*
<Button
 ...
 android:onClick="clickTest"
 />
*/

//再在对应需要响应的布局中添加同名的Click方法
public void clickTest(View view) {
        Log.e(TAG, "clickTest");
    }

3.2 长按事件

  • 通过setLongClickListener()方法实现
buttonClick.setOnLongClickListener(view -> {
 //长按事件
    Log.e(TAG, "onLongClick");
    //长按事件的返回值为true,也不会再往onClick传递
    return false;
});

3.3 触摸事件

  • onTouch()事件,优先级最高,并且可以通过motionEvent参数来获取点击的类型,按下的坐标,按下的状态等信息,做更多的多样化处理
buttonClick.setOnTouchListener((view, motionEvent) -> {
    //触摸事件
    int action = motionEvent.getAction();
    String actionStr = getActionStr(action);
    Log.e(TAG, actionStr);
    //这里返回为true的话代表事件被消费不再往下传递
    return false;
});

private String getActionStr(int action) {
String str;
switch (action) {
    case MotionEvent.ACTION_DOWN:
        str = "ACTION_DOWN";
        break;
    ...
}
return str;
}

三、EditText

1.常用属性

名称作用
android:hint提示文字
android:textColorHint提示文字的颜色
android:inputType输入内容的类型-密码/文字/数字等
android:drawableXxxx在输入框的指定方位添加drawable
android:drawablePadding设置图片与输入内容的间距大小
```android:paddingXxxx内容与边框的间距大小

其它很多属性与TextView和Button都是通用的就不一一列举

<EditText
     android:id="@+id/et_pass_word"
     ...
     android:hint="Please input your password !"  提示文字
     android:drawableLeft="@drawable/ic_password_24"  icon图标
     android:backgroundTint="@color/btn_color_selector"  
     android:drawablePadding="15dp"  图标边距
     android:inputType="textPassword"/>  输入的内容的类型

输入内容结合Button可以做出按下按钮实现查看密码的功能

passWord = findViewById(R.id.et_pass_word);
Button seePass = findViewById(R.id.btn_see_pass_word);
seePass.setOnTouchListener((view, motionEvent) -> {
    runOnUiThread(() -> {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            passWord.setInputType(InputType.TYPE_CLASS_TEXT);
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            passWord.setInputType(129);
        }
        //移动光标与刷新
        passWord.setSelection(passWord.getText().length());
        passWord.invalidate();
    });
    return false;
});
}

或者做出输入密码纠错的功能

public void checkPass(View view) {
//点击按钮时获取内容,与需要的值比对,进而验证账号密码是否是正确的
    if (!"iFinder".equals(userName.getText()) || !"123".equals(passWord.getText())) {
        runOnUiThread(() -> {
            userName.setTextColor(getResources().getColor(R.color.red));
            passWord.setTextColor(getResources().getColor(R.color.red));
            userName.invalidate();
            passWord.invalidate();
        });
    }
}

最终效果

四、ImageView

1.主要属性

属性作用
android:src设置图片的资源
android:scaleType设置图片的缩放类型
android:maxHeight最大高度
android:maxWidth最大宽度
android:adjustViewBounds是否调整View的界限

1.1缩放类型–scaleType

类型名具体作用
fitStart保持宽高比缩放图片,直到较长的边与View的边长相等,完成后将图片放在View的左上角
fitCenter默认值,缩放规则同上,但完成后放于View中间
fitEnd缩放规则同上,但我弄成后放于View右下角
fitXY对图像的XY方向分别进行缩放,使得图片完全填满View,但是宽高比可能会发生改变
center保持原图大小,显示在View的中心,原图大于View大小时直接进行剪裁处理
centerCrop保持宽高比缩放图片直至完全覆盖View,可能会出现图片显示不全
centerInside保持宽高比缩放图片,只要能完全显示就停止,不一定适配View宽高
matrix不改变原图的大小,从View的左上角开始绘制原图,超出部分做相应的剪裁处理
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_user_500"
    android:scaleType="matrix"
    android:maxHeight="300dp"
    android:maxWidth="300dp"
    android:adjustViewBounds="true"/>

五、ProgressBar

1.常用属性

标签说明
android:max进度条可显示的最大值
android:progress进度条当前已完成值
android:indeterminate是否显示精确进度, 不显示时有类似跑马灯的等待效果
android:style设置进度条的风格

2.两种简单进度条

<ProgressBar
   android:id="@+id/pb_show_hint"
   android:layout_width="match_parent"
   android:layout_height="50dp"/>
//默认时圆圈等待的样式
<ProgressBar
    ...id 宽高
    style="?android:attr/progressBarStyleHorizontal"  水平进度样式
    android:max="100"  最大显示进度
    android:indeterminate="true" 是否精确进度/>

结合ProgressBar自带的一些get/set属性,可以轻松的在代码当中操作相应的值
这里结合之前的Button控件的Click事件实现ProgressBar的显示与隐藏, 进度的控制

public void barShowHint(View view) {
	//设置控件的可见性
    pbShowHint.setVisibility(pbShowHint.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}

public void barAddProgress(View view) {
	//设置控件的当前进度,当然实际使用中的情况要比这个更加复杂
    int progress = pbAddProgress.getProgress();
    pbAddProgress.setProgress(progress >= 100 ? 0 : progress + (int)(Math.random() * 10));
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值