【Android开发】简单View组件开发

View

  • 处理文本内容 TextView
  • 被点击的View Button
  • 处理图片内容的View ImageView
  • 接收用户信息输入的View EditView
  • 进度条类的VIew ProgressBar

通用属性

属性 常用可选值
android:layout_width
android:layout_height
match_parent 填充整个容器
wrap_content 根据所含内容确定
正整数单位dp 精确大小
android:id @id/valName 使用已存在的id
@+id/valName 添加新的id
android:layout_margin 正整数dp 和相邻控件或边缘的距离
android:padding 正整数dp 控件内容距离控件边缘的距离
android:background 十六进制的颜色值 颜色作为背景
@mipmap/resourceId 图片作为背景
android:layout_gravity
android:gravity
android:layout_gravity 相对于父容器的位置
android:gravity控件里面的内容的位置
center_horizontal center_vertical center
left right bottom top
android:visibility visible 可见状态
invisible 不可见状态,但是保留控件位置
gone 不可见状态,也不保留位置

TextView

  • 对长文本进行显示处理
  • 支持Html代码
  • 内容有样式,链接效果

继承关系

View -> TextView -> Button
-> EditView

<?xml version="1.0" encoding="utf-8"?>

<!--ScrollView 滚动显示,但是只能放置一个子控件-->
<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=".TextActivity"
    android:orientation="vertical"
    >

    <!--
        android:textSize 设置字体大小
        android:textColor 设置字体颜色
        android:lineSpacingMultiplier 倍距
        android:lineSpacingExtra 精确行距
        android:lines 设置显示行数
        android:singleLine 只显示一行
        android:ellipsize 省略号的位置 start middle end marquee 跑马灯
        android:focusable="true" 设置可以获取焦点
        android:focusableInTouchMode="true" 设置在触摸时可以获取焦点
        android:marqueeRepeatLimit="marquee_forever" 跑马灯的重复次数
    -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/long_text"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        />

</LinearLayout>

EditView

    <!--
        android:inputType 输入类型
        textPassword 密码
        number 只能是正整数
        numberSigned 整数
        numberDecimal 小数
        numberDecimal | numberSigned 同时使用两种属性
        
        android:hint 输入提示
        android:textColorHint 输入提示的文字颜色
        android:maxLength 输入文字的最大长度
    -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="25dp"
        android:inputType="text"
        android:hint="Name and Surname"
        android:gravity="center"
        android:textColorHint="#cccccc"
        android:background="@mipmap/border"
        android:maxLength="12"/>

Button

  • 自定义内部类
  • 匿名内部类
  • 当前Activity去实现事件接口
  • 在布局文件中添加点击事件属性
<?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=".ButtonActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通过自定义内部类实现点击事件"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通过匿名内部类实现点击事件"/>

    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="通过当前Activity实现点击事件接口"/>

    <Button
        android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="在xml文件中绑定1"
        android:onClick="myClick"/>

    <Button
        android:id="@+id/btn5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="在xml文件中绑定2"
        android:onClick="myClick"/>

</LinearLayout>
package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button

class ButtonActivity : AppCompatActivity(), View.OnClickListener {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_button_acticity) // 应该先添加布局文件之后才能获取布局文件中定义的控件

        //一、自定义内部类
        //1.根据id获取按钮
        var btn1 : Button = findViewById(R.id.btn1)
        var mcl = MyClickListener()
        btn1.setOnClickListener(mcl) // 3.为按钮注册点击事件监听器

        //二、匿名内部类 只使用一次时适用
        val btn2 = findViewById<Button>(R.id.btn2)
        btn2.setOnClickListener{
    
     Log.e("Tag", "匿名内部类监听器对象的按钮") }

        //三、当前Activity去实现事件接口
        val btn3 = findViewById<Button>(R.id.btn3)
        btn3.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
    
    
        Log.e("Tag", "当前Activity去实现事件接口的按钮")
    }

    // 2. 创建内部类,继承onClickListener接口
    internal inner class MyClickListener : View.OnClickListener {
    
    
        override fun onClick(v: View?) {
    
    
            Log.e("Tag", "内部类监听器对象的按钮")
        }

    }

    //四、参数:被点击的控件对象
    fun myClick(v: View?){
    
    
        if (v != null) {
    
    
            when(v.id){
    
    
                R.id.btn4 -> Log.e("Tag", "在布局文件中添加点击事件属性1")
                R.id.btn5 -> Log.e("Tag", "在布局文件中添加点击事件属性2")
            }
        }
    }
}

ImageView

用来显示和控制图像的控件,可以对它进行放大缩小旋转等操作。

    <!--
        android:src 指定前景图片资源 会保持自身的比例不会变形
        android:background 设置背景
    -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/add_photo"
        android:layout_margin="10dp"/>

    <!--
        没有text属性
    -->
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/add_photo"/>

ProgressBar

进度条,默认情况下是圆形,没有刻度,只是一个不断旋转的动画效果。通过设置style,可以显示传统的水平带刻度进度条。

<?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=".ProgressBarConvert"
    android:orientation="vertical">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <!--
        style 风格
        android:progress 设置进度
        android:max 设置最大值
    -->
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:progress="30"
        android:max="200"/>

    <!--
        android:indeterminate 设置进度条一直滚动
    -->
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminate="true"/>

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        />

</LinearLayout>
package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ProgressBar

class ProgressBarActivity : AppCompatActivity() {
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_progress_bar)

        var progressBar = findViewById<ProgressBar>(R.id.progress)
        progressBar.progress = 80

        //在Android中,4.0之后是不能直接在线程中操作控件的
        //进度条是一个特例
        object : Thread() {
    
    
            override fun run(){
    
    
                for(i in 1..100){
    
    
                    progressBar.progress = i
                    try {
    
    
                        sleep(30)
                    } catch (e: InterruptedException) {
    
    
                        e.printStackTrace()
                    }
                }
            }
        }
    }
}

判空

	<EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="25dp"
        android:inputType="text"
        android:hint="Name and Surname"
        android:gravity="center"
        android:textColorHint="#cccccc"
        android:background="@mipmap/border"/>

	<EditText
	    android:id="@+id/pwd"
	    android:layout_width="match_parent"
	    android:layout_height="68dp"
	    android:layout_marginLeft="30dp"
	    android:layout_marginRight="30dp"
	    android:layout_marginTop="25dp"
	    android:inputType="textPassword"
	    android:hint="Password"
	    android:gravity="center"
	    android:textColorHint="#cccccc"
	    android:background="@mipmap/border"
	    android:maxLength="12"/>

	<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="20dp"
        android:background="@mipmap/btn"
        android:onClick="register"/>
    fun register(v : View?){
    
    
        val nameEdt = findViewById<EditText>(R.id.name)
        val pwdEdit = findViewById<EditText>(R.id.pwd)
        val progressBar = findViewById<ProgressBar>(R.id.pro_bar)
        val name = nameEdt.text.toString()
        val pwd = pwdEdit.text.toString()
        if (name == "" || pwd == "") {
    
    
            //无焦点提示
            //参数1:环境上下文 参数2:提示性文本 参数3:提示持续时间
            Toast.makeText(this, "姓名或密码不能为空", Toast.LENGTH_SHORT)
        } else {
    
    
            //都不为空,出现进度条
            progressBar.visibility = View.VISIBLE
            object : Thread() {
    
    
                override fun run() {
    
    
                    for (i in 0..100) {
    
    
                        progressBar.progress = i
                        try {
    
    
                            sleep(30)
                        } catch (e: InterruptedException) {
    
    
                            e.printStackTrace()
                        }
                    }
                }
            }.start()
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42020386/article/details/112761366