常见控件的使用方法「终于解决」

常见控件的使用方法「终于解决」「这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战」 TextView 在界面上显示一段文本信息。 指定文字的对齐方式 可以使用‘|’来同时指定gravity的多个参数。 也可

「这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战

TextView

在界面上显示一段文本信息。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/text_view" android:text="This is TextView"/>
</LinearLayout>

在这里插入图片描述 指定文字的对齐方式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/text_view" android:gravity="center" android:text="This is TextView"/>
</LinearLayout>

在这里插入图片描述 可以使用‘|’来同时指定gravity的多个参数。

也可以指定文字的大小(textSize,单位sp)和颜色(textColor),颜色可以用取色器去提取自己想要的颜色,然后填入16进制数据就好。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/text_view" android:gravity="center" android:textSize="20sp" android:textColor="#00ff00" android:text="This is TextView"/>
</LinearLayout>

在这里插入图片描述

Button

按钮,这是一个非常熟悉的控件,其实Button是继承自TextView,因为同样的你可以在指定Text属性。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="button"/>
</LinearLayout>

在这里插入图片描述 会发现button全是大写,可以通过textAllcaps属性进行修改。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:textAllCaps="false" android:text="button"/>
</LinearLayout>

在这里插入图片描述 显然,button也是可以用来响应事件的。

        Button button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Click the button",Toast.LENGTH_SHORT).show();
            }
        });

EditText

这个控件可以和用户进行交互(当然button也可以),它允许用户在控件里输入和编辑内容,并且可以在程序中对这些内容处理。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
   <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>

部分输入框还有提示功能,这里用hint属性实现。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
   <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Type Something here"/>
</LinearLayout>

当输入的文本过长时,我们可以使用maxLines来指定最大显示的行数(其他的文本可以通过滑动来看到)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
   <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="2" android:hint="Type Something here"/>
</LinearLayout>

也可以对这些数据进行一些交互,如通过Toast将信息显示出来。

Button button=findViewById(R.id.button);
        EditText editText=findViewById(R.id.edit_text);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,editText.getText().toString(),Toast.LENGTH_SHORT).show();
            }
        });

ImageView

imageView是一个在界面上展示图片的一个控件,图片一般放在drawable目录下,但是这个目录没有分辨率,于是可以创建一个drawable-xhdpi目录,放在这下面。 需要注意的是图片名字不能以数字开头,一般小写字母加下划线。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
    <ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/cute_girl"/>
</LinearLayout>

ProgressBar

进度条也是一个常见的控件,表示我们的程序正在加载一些数据。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
    <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressBar"/>
</LinearLayout>

在这里插入图片描述

但是一般加载数据的时候,加载数据完成了进度条也就消失了,那么要怎样做到这个效果呢?,android给了我们每个控件一个visibility属性。 这个属性有三个值:

  1. visible(默认,表示可见)
  2. invisible(不可见,当时控件还在,相当于透明)
  3. gone(消失)
        Button button=findViewById(R.id.button);
        ProgressBar progressBar=findViewById(R.id.progressBar);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.INVISIBLE);
            }
        });

也可以改变它的style

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
    <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="finished" android:textAllCaps="false" android:layout_gravity="center"/>
    <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressBar"/>
</LinearLayout>

这样就变成了水平进度条。 在这里插入图片描述 还可以设置它的一个最大值,可以通过这个最大值来体现进度。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
    <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="finished" android:textAllCaps="false" android:layout_gravity="center"/>
    <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:id="@+id/progressBar"/>
</LinearLayout>
        Button button=findViewById(R.id.button);
        ProgressBar progressBar=findViewById(R.id.progressBar);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setProgress(progressBar.getProgress()+10);
            }
        });

AlertDialog

对话框也是一个很常见的控件,用来提示用户某些信息,当出现弹窗时Activity会处于onPause状态。 和别的不同的是,这个对话框可以在Acitivity中直接定义书写。

        Button button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建一个对话框
                AlertDialog.Builder  dialog=new AlertDialog.Builder(MainActivity.this);
                //将对话框设置为不可取消
                dialog.setCancelable(false);
                //设置对话框的标题
                dialog.setTitle("This is dialog");
                //设置对话框的内容
                dialog.setMessage("Something is important");
                dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
    }

ProgressDialog

当需要提示用户当前进度时就可以用这个控件了,这个控件会弹出一个进度条来提示用户当前进度。使用方法和AlertDialog差不多,也可以在Activity中创建。

        Button button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建一个进度弹窗
                ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
                progressDialog.setTitle("this is a progressDialog");
                progressDialog.setCancelable(true);
                progressDialog.show();
            }
        });

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/13138.html

(0)

相关推荐

  • MySQL默认隔离级别为什么是RR

    MySQL默认隔离级别为什么是RR曾多次听到“MySQL为什么选择RR为默认隔离级别”的问题,其实这是个历史遗留问题,当前以及解决,但是MySQL的各个版本沿用了原有习惯。历史版本中的问题是什么,本次就通过简单的测试来说明一下。 1、

    2023-02-14
    88
  • Python 中,单引号与双引号有什么区别?

    Python 中,单引号与双引号有什么区别?Python 中单引号和双引号是两种表示字符串的方式。在日常的 Python 编程中,单引号和双引号的使用不同,有的时候甚至会影响程序的运行结果。

    2024-04-20
    21
  • 大麦网技术二三事「建议收藏」

    大麦网技术二三事「建议收藏」杭州周杰伦2017年项目,大麦网抢票系统抢瘫了,据传阿里内部炸了锅,大麦在阿里序列里直接进入了被鄙视链的第一名,江湖上也是声名狼藉。作为大麦故人(大麦网前技术总监及初创团队核心),我已经是不止一次的在各种场合听到诸如“大麦网技术水平太烂了”、“大麦基本没什么技术含量”、“大麦的…

    2023-08-13
    73
  • 绝望之塔第七层(第一章等到绝望第二)

    绝望之塔第七层(第一章等到绝望第二)

    2023-09-27
    89
  • MYSQL基础语句(自我记忆)

    MYSQL基础语句(自我记忆)一.操作数据库 1.查看所有数据库 : show databases; 2.创建数据库 : create database name(数据库名); 3.删除数据库 : drop database na

    2023-02-04
    113
  • MySQL学习笔记(21):优化磁盘IO

    MySQL学习笔记(21):优化磁盘IO本文更新于2020-04-05,使用MySQL 5.7,操作系统为Deepin 15.4。 使用符号连接分布IO 利用操作系统的符号连接,将不同的数据库、表、索引指向不同的物理磁盘,从而达到分布磁盘I

    2023-03-24
    112
  • 数据库读写一致性_redis与数据库的一致性

    数据库读写一致性_redis与数据库的一致性1 导读 数据的一致性是数据准确的重要指标,那如何实现数据的一致性呢?本文从事务特性和事务级别的角度和大家一起学习如何实现数据的读写一致性。 2 一致性 1.数据的一致性:通常指关联数据之间的逻辑关系

    2023-06-07
    100
  • MyISAM索引底层是什么结构_小提琴进阶

    MyISAM索引底层是什么结构_小提琴进阶本文源码:GitHub&#183;点这里 || GitEE&#183;点这里 一、索引简介 1、基本概念 首先要明确索引是什么:索引是一种数据结构,数据结构是计算机存储、组织数据的方式

    2023-03-08
    115

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注