[Desarrollo de Android] Desarrollo de componentes de Vista simple

Ver

  • Procesamiento de contenido de texto TextView
  • Ver botón en el que se hizo clic
  • Ver ImageView que maneja el contenido de la imagen
  • Ver EditView que recibe la entrada de información del usuario
  • VIew ProgressBar de la clase de barra de progreso

Atributos generales

Atributos Valores opcionales de uso común
android: layout_width
android: layout_height
match_parent llenar todo el contenedor
wrap_content el contenido se determina de acuerdo con
una unidad entera positiva tamaño exacto dp
android: id @ id / valName Use el id
@ + id / valName existente para agregar un nuevo id
android: layout_margin La distancia entre un dp entero positivo y controles o bordes adyacentes
android: relleno Entero positivo dp La distancia entre el contenido del control y el borde del control
android: fondo Valor de color hexadecimal color como fondo
@ mipmap / resourceId imagen como fondo
android: layout_gravity
android: gravedad
android: layout_gravity relativo a la posición del contenedor principal
android: control de gravedad posición del contenido
center_horizontal center_vertical center
left right bottom top
android: visibilidad estado visible estado
invisible invisible, pero mantiene la posición de control en
estado invisible, no mantiene la posición

Vista de texto

  • Procesamiento de visualización de texto largo
  • Soporte de código Html
  • El contenido tiene estilos, efectos de enlace.

Herencia

Ver -> TextView -> Botón
-> 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>

Editar vista

    <!--
        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"/>

Botón

  • Clase interna personalizada
  • Clase interna anónima
  • La actividad actual para implementar la interfaz de eventos
  • Agregar atributos de evento de clic al archivo de diseño
<?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

Los controles utilizados para mostrar y controlar la imagen se pueden acercar, alejar y girar.

    <!--
        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"/>

Barra de progreso

La barra de progreso, por defecto, es circular, sin escala, solo un efecto de animación que gira continuamente. Al establecer el estilo, se puede mostrar la barra de progreso tradicional en escala horizontal.

<?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()
                    }
                }
            }
        }
    }
}

Vacío

	<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()
        }
    }

Supongo que te gusta

Origin blog.csdn.net/weixin_42020386/article/details/112761366
Recomendado
Clasificación