几种Android常用控件的使用方法


Android给我们提供了大量的UI控件,首先我们新建一个UIWidgetTest项目,默认Android Studio自动创建Activity,Activity名和布局名都使用默认值

TestView

它主要用于在界面上显示一段文本信息

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is TextView"/>
</LinearLayout>

在这里插入图片描述

android:id给当前控件定义了一个唯一的标识符

android:layout_width和android:layout_height指定了控件的宽度和高度,Android中所有的控件都具有这两个属性
可选值:match_parent,wrap_content和固定值
match_parent表示让当前控件的大小和父布局的大小一样
wrap_content:表示让当前控件的大小能够刚好包含住里面的内容,也就是控件内容决定当前控件的大小
固定值表示指定一个固定的尺寸,单位一般用dp

我们好像没看出TextView的宽度和屏幕一样宽的,这是由于TextView中的文字默认是居左上角对齐的,虽然TextView的宽度充满了整个屏幕,可是由于文字内容不够长,所以从效果上完全看不出来,现在修改对齐方式,如下:

<TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="This is TextView"/>

在这里插入图片描述

android:gravity来指定文字的对齐方式,可选值有top,bottom,start,end,center等

另外我们还可以对TextView中文字的颜色和大小进行修改,如下:

<TextView
		android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#00ff00"
        android:textSize="24sp"
        android:text="This is TextView"/>

在这里插入图片描述

android:textColor属性可以指定文字的颜色
android:textSize属性可以指定文字的大小,文字大小要使用sp作为单位

Button

Button是程序用于和用户进行交互的一个重要控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <!-- <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#00ff00"
        android:textSize="24sp"
        android:text="This is TextView"/>
-->
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"/>
</LinearLayout>

在这里插入图片描述
可能大家发现我们定义的是Button,但是图片中是BUTTON,这是因为Android系统默认会将按钮上的英文字母全部转换为大写,可能是认为按钮上的内容是比较重要叭,如果你想按照你写的来显示,可以加上android:textAllCaps=“false” 这个属性,这样系统就会保留你指定的原始文字内容

接下来,我们可以在MainActivity中为Button的点击事件注册一个监听器

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button=findViewById<Button>(R.id.button)
        button.setOnClickListener{
            //在此处添加逻辑
        }
    }
}

EditText

EditText是程序用于和用户进行交互的另一个重要控件,它允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理.应用场景:发短信,聊QQ

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

在这里插入图片描述
一些人性化的软件会在输入框里显示一些提示性文字,一旦用户输入任何内容,这些提示性文字就会消失

        android:hint="Type something here"

随着内容越来越多,EditText会被不断拉长,这是因为EditText高度指定的是wrap_content,可以通过设定最大行数,当输入内容超过2行时,文本就会向上翻滚

android:maxLines="2"

ImageView

ImageView是用于在界面上展示图片的一个控件,在res目录下新建一个drawable-xxhdpi目录,将事先准备的图片放到该目录下

<ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1"/>

在这里插入图片描述

ProgressBar

ProgressBar用于在界面上显示一个进度条,表示我们的程序正在加载一些数据

<ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

在这里插入图片描述
可以修改ProgressBar样式,

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

在这里插入图片描述

AlertDialog

AlertDialog可以在当前界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽其他控件的交互能力,因此AlertDialog一般用于提示一些非常重要的内容或者警告信息
比如为了防止用户误删重要内容,在删除前弹出一个确认对话框

class MainActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button:Button=findViewById(R.id.button)
        val editText:EditText=findViewById(R.id.editText)
        val imageView:ImageView=findViewById(R.id.imageView)
        val progressBar:ProgressBar=findViewById(R.id.progressBar)
        button.setOnClickListener{
            //在此处添加逻辑
//            val inputText=editText.text.toString()
//            Log.d("MainActivity",inputText)
//            Toast.makeText(this,inputText,Toast.LENGTH_LONG).show()

            //imageView.setImageResource(R.drawable.img_2)
//            if(progressBar.visibility==View.VISIBLE){
//                progressBar.visibility=View.GONE
//            }else{
//                progressBar.visibility=View.VISIBLE
//            }
            //progressBar.progress=progressBar.progress+10
            AlertDialog.Builder(this).apply {
                setTitle("this is Dialog")
                setMessage("something important")
                setCancelable(false)
                setPositiveButton("OK"){
                    dialog,which->
                }
                setNegativeButton("Cancel"){
                    dialog,which->
                }
                show()
            }
        }
    }

在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>