IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android开发学习总结day1-2 -> 正文阅读

[移动开发]Android开发学习总结day1-2

perface

In this article I will show you about button ,EditText and ImageView
for this is the second article of the series I will not write as such detailed as the first but more detailed than other similiar article

Button

if we see the code we will find that button enxtends TextView which means we can use almost every attribute inTextView
在这里插入图片描述

example code

在这里插入代码片

background problem

if your version is new you will find that you can’t change the background in button

resolve

we can add Bridge in theme.xml to resolve this problem
在这里插入图片描述

background image

background image is in the background so you can do it like this

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

then wirte the code in the btn_selector.xml

    <item android:drawable="@drawable/ic_baseline_account_circle_24" android:state_pressed="true"></item>

then you just need to use btn_selector.xml in the activity_main.xml

<Button
        android:text="button"
        android:textColor="@color/black"
        android:background="@drawable/btn_selector"
        android:textSize="20sp"
        android:textStyle="italic"
        android:layout_width="150dp"
        android:layout_height="150dp">
    </Button>

if you click the button you will find:
在这里插入图片描述

backgroundTint

same as background

Button Event

first you should get button

private final String TAG = "btn!";
Button btn1 = findViewById(R.id.btn1);

在这里插入图片描述

ClickEvent

when you click the button it will happened
by the way there have two ways to set

1.use Listener

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "onClick: happened");
            }
        });
lambda
        btn1.setOnClickListener(view -> Log.e(TAG, "onClick: happened"));

2.NO Listener

在这里插入图片描述

    public void clickEvent(View view) {
        Log.e(TAG, "clickEvent: clickEvent");
    }

LongClickEvent

when you press and hold the button it will happened

1.use Listener

        btn1.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Log.e(TAG, "onLongClick:happened");
                return false;
            }
        });
lambda

        btn1.setOnLongClickListener(view -> {
            Log.e(TAG, "onLongClick:happened");
            return false;
        });

TouchEvent

when you touch the button it will happened

1. use Listener

        btn1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e(TAG, "onTouch:happened");
                return false;
            }
        });
lambda
        btn1.setOnTouchListener((view, motionEvent) -> {
            Log.e(TAG, "onTouch:happened");
            return false;
        });

EditText

    <EditText
        android:id="@+id/text1"
        android:hint="please enter your name"
        android:background="@color/teal_200"
        android:padding="10dp"
        android:textColor="@color/purple_500"
        android:inputType="text"
        android:drawablePadding="10dp"
        android:layout_width="260dp"
        android:drawableLeft="@drawable/btn_selector"
        android:layout_marginLeft="20dp"
        android:layout_height="60dp">
    </EditText>

Attributes

AttriNameexplainexample
idthe name of the controller and you can get the controller by it’s idandroid:id="@+id/text1"
hintthe text which can show by default in the controllerandroid:hint=“please enter your name”
backgroundthe background of the controller you can set it with color or imagesandroid:background="@color/teal_200"
paddingthe padding of the controller which means that the controller will have inner widthandroid:padding=“10dp”
textColorthe text color of the text in the controllerandroid:textColor="@color/purple_500"
inputTypeyou can set the type that users must enter the right type or they can’t enter anythingandroid:inputType=“text”
drawablePaddingif you find your drawable images are close to your words you can set this attribute to make a fitness designandroid:drawablePadding=“10dp”
layout_widththe basic attribute which can set the width of the controllerandroid:layout_width=“260dp”
layout_marginLeftthat means your controller will have a outside width away to other controllers ,what’s more you can set top/bottom/rightandroid:layout_marginLeft=“20dp”
drawableLeftyou can set this attribute to make sure you drawable images is in the left side,what’s more you can set top/bottom/rightandroid:drawableLeft="@drawable/btn_selector"
the basic attribute which can set the width of the controllerandroid:layout_height=“60dp”

show result

在这里插入图片描述

Event

we always need to get the users enter

//get edittext
        EditText viewById = findViewById(R.id.text1);
        Log.e(TAG,"EditText:"+ viewById);
// get button
        Button btn1 = findViewById(R.id.btn1);
// user click
        btn1.setOnClickListener(view ->{
        // get user enter
            String userInput = viewById.getText().toString();
            Log.e(TAG, "user enter => "+userInput );
//            Log.e(TAG, "onClick: happened")
        } );

as you can see there have a button and a EditText and I write an event which if users enter something and than click the button we will get the users’ enter!
在这里插入图片描述

在这里插入图片描述

ImageView

    <ImageView
        android:src="@drawable/btn_selector"
        android:scaleType="fitCenter"
        android:layout_width="150dp"
        android:layout_height="150dp"></ImageView>

show result

在这里插入图片描述

Attributes

AttriNameexplainexample
srcyou can set the images by srcandroid:src="@drawable/btn_selector"
scaleTypeI suggest you to use “fitXY” or “fitCenter”android:scaleType=“fitCenter”

some not necessary scaleType

<attr name="scaleType">
            <!-- Scale using the image matrix when drawing. See
                 {@link android.widget.ImageView#setImageMatrix(Matrix)}. -->
            <enum name="matrix" value="0" />
            <!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#START}. -->
            <enum name="fitStart" value="2" />
            <!-- Scale the image using {@link android.graphics.Matrix.ScaleToFit#END}. -->
            <enum name="fitEnd" value="4" />
            <!-- Center the image in the view, but perform no scaling. -->
            <enum name="center" value="5" />
            <!-- Scale the image uniformly (maintain the image's aspect ratio) so both dimensions
                 (width and height) of the image will be equal to or larger than the corresponding
                 dimension of the view (minus padding). The image is then centered in the view. -->
            <enum name="centerCrop" value="6" />
            <!-- Scale the image uniformly (maintain the image's aspect ratio) so that both
                 dimensions (width and height) of the image will be equal to or less than the
                 corresponding dimension of the view (minus padding). The image is then centered in
                 the view. -->
            <enum name="centerInside" value="7" />
        </attr>
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-03-12 17:41:04  更:2022-03-12 17:41:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年10日历 -2024/10/31 13:28:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码