Android 几种常用组件介绍及UI结构介绍

bAndroid应用界面开发

  • 应用结构分析

  • 基本组件认识

  • Text View 和 EditText 的功能与应用

  • Button 和 ImageButton 的功能与应用

  • 消息提示框 Toast

应用结构分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UGX76BbZ-1600624307028)(预习2.assets/image-20200916144246341.png)]

应用组件

应用组件是 Android 应用的基本构建块。每个组件都是一个入口点,系统或用户可通过该入口点进入您的应用。有些组件会依赖于其他组件。

共有四种不同的应用组件类型:

  • Activity
  • 服务
  • 广播接收器
  • 内容提供程序

每种类型都有不同的用途和生命周期,后者会定义如何创建和销毁组件。以下部分将介绍应用组件的四种类型。

  • Activity 【理解为活动页面】

    ==Activity 是与用户交互的入口点。它表示拥有界面的单个屏幕。==例如,电子邮件应用可能有一个显示新电子邮件列表的 Activity、一个用于撰写电子邮件的 Activity 以及一个用于阅读电子邮件的 Activity。尽管这些 Activity 通过协作在电子邮件应用中形成一种紧密结合的用户体验,但每个 Activity 都独立于其他 Activity 而存在。因此,其他应用可以启动其中任何一个 Activity(如果电子邮件应用允许)。例如,相机应用可以启动电子邮件应用内用于撰写新电子邮件的 Activity,以便用户共享图片。Activity 有助于完成系统和应用程序之间的以下重要交互:追踪用户当前关心的内容(屏幕上显示的内容),以确保系统继续运行托管 Activity 的进程。了解先前使用的进程包含用户可能返回的内容(已停止的 Activity),从而更优先保留这些进程。帮助应用处理终止其进程的情况,以便用户可以返回已恢复其先前状态的 Activity。提供一种途径,让应用实现彼此之间的用户流,并让系统协调这些用户流。

启动组件

在四种组件类型中,==有三种(Activity、服务和广播接收器)均通过异步消息 Intent 进行启动。==Intent 会在运行时对各个组件进行互相绑定。您可以将 Intent 视为从其他组件(无论该组件是属于您的应用还是其他应用)请求操作的信使。

需使用 Intent 对象创建 Intent,该对象通过定义消息来启动特定组件(显式 Intent)或特定的组件类型(隐式 Intent)。

对于 Activity 和服务,Intent 会定义要执行的操作(例如,查看发送某内容),并且可指定待操作数据的 URI,以及正在启动的组件可能需要了解的信息。例如,Intent 可能会传达对 Activity 的请求,以便显示图像或打开网页。在某些情况下,您可以通过启动 Activity 来接收结果,这样 Activity 还会返回 Intent 中的结果。例如,您可以发出一个 Intent,让用户选取某位联系人并将其返回给您。返回 Intent 包含指向所选联系人的 URI。

对于广播接收器,Intent 只会定义待广播的通知。例如,指示设备电池电量不足的广播只包含指示*“电池电量不足”*的已知操作字符串。

与 Activity、服务和广播接收器不同,内容提供程序并非由 Intent 启动。相反,它们会在成为 ContentResolver 的请求目标时启动。内容解析程序会通过内容提供程序处理所有直接事务,因此通过提供程序执行事务的组件便无需执行事务,而是改为在 ContentResolver 对象上调用方法。这会在内容提供程序与请求信息的组件之间留出一个抽象层(以确保安全)。

每种组件都有不同的启动方法:

  • 如要启动 Activity,您可以向 startActivity()startActivityForResult() 传递 Intent(当您想让 Activity 返回结果时),或者为其安排新任务。
  • 在 Android 5.0(API 级别 21)及更高版本中,您可以使用 JobScheduler 类来调度操作。对于早期 Android 版本,您可以通过向 startService() 传递 Intent 来启动服务(或对执行中的服务下达新指令)。您也可通过向将 bindService() 传递 Intent 来绑定到该服务。
  • 您可以通过向 sendBroadcast()sendOrderedBroadcast()sendStickyBroadcast() 等方法传递 Intent 来发起广播。
  • 您可以通过在 ContentResolver 上调用 query(),对内容提供程序执行查询。

清单文件

在 Android 系统启动应用组件之前,==系统必须通过读取应用的清单文件 (AndroidManifest.xml) 确认组件存在。===您的应用必须在此文件中声明其所有组件,该文件必须位于应用项目目录的根目录中。

除了声明应用的组件外,清单文件还有许多其他作用,如:

  • 确定应用需要的任何用户权限,如互联网访问权限或对用户联系人的读取权限。
  • 根据应用使用的 API,声明应用所需的最低 API 级别
  • 声明应用使用或需要的硬件和软件功能,如相机、蓝牙服务或多点触摸屏幕。
  • 声明应用需要链接的 API 库(Android 框架 API 除外),如 Google 地图库

声明组件

清单文件的主要任务是告知系统应用组件的相关信息。例如,清单文件可按如下所示声明 Activity:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
    <application android:icon="@drawable/app_icon.png" ... >
        <activity android:name="com.example.project.ExampleActivity"
                  android:label="@string/example_label" ... >
        </activity>
        ...
    </application>
</manifest>

<application> 元素中,android:icon 属性指向标识应用的图标所对应的资源。

<activity> 元素中,android:name 属性指定 Activity 子类的完全限定类名,android:label 属性指定用作 Activity 的用户可见标签的字符串。

您必须使用以下元素声明所有应用组件:

  • Activity 的 <activity> 元素。
  • 服务的 <service> 元素。
  • 广播接收器的 <receiver> 元素。
  • 内容提供程序的 <provider> 元素。

如果未在清单文件中声明源代码中包含的 Activity、服务和内容提供程序,则这些组件对系统不可见,因此也永远不会运行。不过,您可以 BroadcastReceiver 对象的形式,在清单中声明或在代码中动态创建广播接收器;以及通过调用 registerReceiver(),在系统中注册广播接收器。

如上文启动组件中所述,您可以使用 Intent 来启动 Activity、服务和广播接收器。您可以通过在 Intent 中显式命名目标组件(使用组件类名)来使用 Intent。您还可使用隐式 Intent,通过它来描述要执行的操作类型和待操作数据(可选)。借助隐式 Intent,系统能够在设备上找到可执行该操作的组件,并启动该组件。如果有多个组件可以执行 Intent 所描述的操作,则由用户选择使用哪一个组件。

声明组件功能

如上文启动组件中所述,==可以使用 Intent 来启动 Activity、服务和广播接收器。==您可以通过在 Intent 中显式命名目标组件(使用组件类名)来使用 Intent。您还可使用隐式 Intent,通过它来描述要执行的操作类型和待操作数据(可选)。借助隐式 Intent,系统能够在设备上找到可执行该操作的组件,并启动该组件。如果有多个组件可以执行 Intent 所描述的操作,则由用户选择使用哪一个组件。

  • src文件夹:包括了项目的所有包及源文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6FGMLxGZ-1600624307032)(预习2.assets/image-20200916144704243.png)]

Activity 简介

Activity 类是 Android 应用的关键组件,而 Activity 的启动和组合方式则是该平台应用模型的基本组成部分。在编程范式中,应用是通过 main() 方法启动的,而 Android 系统与此不同,它会调用与其生命周期特定阶段相对应的特定回调方法来启动 Activity 实例中的代码。

Activity 的概念

移动应用体验与桌面体验的不同之处在于,用户与应用的互动并不总是在同一位置开始,而是经常以不确定的方式开始。例如,如果您从主屏幕打开电子邮件应用,可能会看到电子邮件列表,如果您通过社交媒体应用启动电子邮件应用,则可能会直接进入电子邮件应用的邮件撰写界面。

Activity 类的目的就是促进这种范式的实现。==当一个应用调用另一个应用时,调用方应用会调用另一个应用中的 Activity,而不是整个应用。==通过这种方式,Activity 充当了应用与用户互动的入口点。您可以将 Activity 实现为 Activity 类的子类。

Activity 提供窗口供应用在其中绘制界面。此窗口通常会填满屏幕,但也可能比屏幕小,并浮动在其他窗口上面。通常,一个 Activity 实现应用中的一个屏幕。例如,应用中的一个 Activity 实现“偏好设置”屏幕,而另一个 Activity 实现“选择照片”屏幕。

大多数应用包含多个屏幕,这意味着它们包含多个 Activity。通常,应用中的一个 Activity 会被指定为主 Activity,这是用户启动应用时出现的第一个屏幕。然后,每个 Activity 可以启动另一个 Activity,以执行不同的操作。例如,一个简单的电子邮件应用中的主 Activity 可能会提供显示电子邮件收件箱的屏幕。主 Activity 可能会从该屏幕启动其他 Activity,以提供执行写邮件和打开邮件这类任务的屏幕。

虽然应用中的各个 Activity 协同工作形成统一的用户体验,但每个 Activity 与其他 Activity 之间只存在松散的关联,应用内不同 Activity 之间的依赖关系通常很小。事实上,Activity 经常会启动属于其他应用的 Activity。例如,浏览器应用可能会启动社交媒体应用的“分享”Activity。

具体介绍

Android 组件继承图

img

Text View 组件

Text View组件继承于 View组件,拥有View组件的全部属性,是一个不可编辑的标签组件,相当于 label 标签

View组件

  • Android中的View组件包含了几乎所有的图形显示组件,TextView和Button实际上都是View类的子类。

img

View 组件的常用属性

No.属性名称方法名称描述
1android:backgroundpublic void setBackgroundResource (int resid)设置组件背景
2android:clickablepublic voidsetClickable (boolean clickable)是否可以产生单击事件
3android:contentDescriptionpublic voidsetContentDescription (CharSequence contentDescription)定义视图的内容描述
4android:drawingCacheQualitypublic voidsetDrawingCacheQuality (int quality)设置绘图时所需要的缓冲区大小
5android:focusablepublic voidsetFocusable (boolean focusable)设置是否可以获得焦点
6android:focusableInTouchModepublic void setFocusableInTouchMode (boolean focusableInTouchMode)在触摸模式下配置是否可以获得焦点
7android:idpublic void setId (int id)设置组件ID
8android:longClickablepublic void setLongClickable (boolean longClickable)设置长按事件是否可用
9android:minHeight定义视图的最小高度
10android:minWidth定义视图的最小宽度
No.属性名称方法名称描述
11android:paddingpublic void setPadding (int left, int top, int right, int bottom)填充所有的边缘
12android:paddingBottompublic void setPadding (int left, int top, int right, int bottom)填充下边缘
13android:paddingLeftpublic void setPadding (int left, int top, int right, int bottom)填充左边缘
14android:paddingRightpublic void setPadding (int left, int top, int right, int bottom)填充右边缘
15android:paddingToppublic void setPadding (int left, int top, int right, int bottom)填充上边缘
16android:scaleXpublic void setScaleX (float scaleX)设置X轴缩放
17android:scaleYpublic void setScaleY (float scaleY)设置Y轴缩放
18android:scrollbarSize设置滚动条大小
19android:scrollbarStylepublic void setScrollBarStyle (int style)设置滚动条样式
20android:visibilitypublic void setVisibility (int visibility)设置是否显示组件
No.属性名称方法名称描述
21android:layout_width定义组件显示的宽度
22android:layout_height定义组件显示的长度
23android:layout_gravity组件文字的对齐位置
24android:layout_margin设置文字的边距
25android:layout_marginTop上边距
26android:layout_marginBottom下边距
27android:layout_marginLeft左边距
28android:layout_marginRight右边距
29android:background设置背景颜色

样例演示

<LinearLayout
        android:background="#09F7F7"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_margin="15dp"
        android:orientation="horizontal"
        >
    <View
        android:id="@+id/red"
        android:background="#FF0033"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_width="wrap_content">
    </View>
    <View
        android:id="@+id/green"
        android:background="#1AE642"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_width="wrap_content">
    </View>
    <View
        android:id="@+id/blue"
        android:background="#0000FF"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_width="wrap_content">
    </View>
    </LinearLayout>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zGnQHkNk-1600624307039)(预习2.assets/image-20200921001404316.png)]

TextView 组件

常用方法:

1.设置字体大小和颜色

android:textSize="20dp"
android:textColor="@color/colorBlack"

2.无法显示时显示省略号

android:maxLines="1"
android:ellipsize="end" //末尾显示省略号

3.设置图片右部显示

android:drawableRight="@drawable/minor"

4.设置跑马灯

android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"

5.设置下划线

//设置中划线
mTv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//设置下划线
mTv1.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

6.设置字体加粗

android:textStyle="bold"

演示

<?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"
    android:orientation="vertical"
    tools:context=".TestViewActivity"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_tes1"
        android:textSize="18sp"
        >
    </TextView>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:textColor="#f00"
        android:textSize="24sp"
        android:text="@string/tv_test2"
        android:layout_marginTop="15dp"
        >

    </TextView>
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="筛选"
        android:textSize="24sp"
        android:drawableRight="@drawable/down"
    >
    </TextView>

    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="36sp"
        android:text="删除线演示"
    >
    </TextView>

    <TextView
        android:id="@+id/tv5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="24sp"
        android:text="下划线演示"
        >


    </TextView>
    <TextView
        android:id="@+id/tv6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="24sp"
        >


    </TextView>


    <TextView
        android:id="@+id/pmd"
        android:text="每一个视图的绘制过程都必须经历三个最主要的阶段,即onMeasure()、onLayout()和onDraw()"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true">
<!--跑马灯演示-->
    </TextView>
</LinearLayout >
package com.example.test3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button mbuttonTest1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mbuttonTest1 = findViewById(R.id.test_button1);
        mbuttonTest1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                跳转到testView演示页面
                Intent   intent = new Intent(MainActivity.this,TestViewActivity.class);
                startActivity(intent);
            }
        });

    }
}

package com.example.test3;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

public class TestViewActivity extends AppCompatActivity {

    private TextView pmdTextView;
    private TextView mTv4,mTv5,mTv6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_view);
        mTv4 = findViewById(R.id.tv4);
        mTv5 = findViewById(R.id.tv5);
        mTv6 = findViewById(R.id.tv6);
        pmdTextView = findViewById(R.id.pmd);
        pmdTextView.setSelected(true);  // 设置聚焦状态


        mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);  // 删除线
        mTv4.getPaint().setAntiAlias(true);  // 去除锯齿


        mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);  // 下划线

        mTv6.setText(Html.fromHtml("<u>下划线2</u> <br> <s>删除线2</s>"));  // 用HTML的方式来实现文本效果
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sV65POis-1600624307041)(预习2.assets/image-20200921010546228.png)]

EditText组件

EditText 组件继承与TextView组件,拥有TextView的全部属性,同时他是一个可以编辑的组件【输入文本框】

<EditText
            android:text=" "//初始游标向后移动一个单位
            android:id="@+id/sendText"
            android:layout_width="0dp"
            android:focusable="true"
            android:layout_height="34dp"
            android:gravity="center_vertical"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="4dp"
            android:background="@drawable/bg_edittext"//editText背景设置
            android:textCursorDrawable="@drawable/color_cursor"//设置游标宽度和颜色
            android:paddingLeft="4dp"
            android:textSize="14dp"
            android:layout_weight="1" />
游标跟随内容:
sendText.setSelection(sendText.getText().length());

输入类型 inputType

android:inputType="none"  
android:inputType="text"  
android:inputType="textCapCharacters"  
android:inputType="textCapWords"  
android:inputType="textCapSentences"  
android:inputType="textAutoCorrect"  
android:inputType="textAutoComplete"  
android:inputType="textMultiLine"  
android:inputType="textImeMultiLine"  
android:inputType="textNoSuggestions"  
android:inputType="textUri"  
android:inputType="textEmailAddress"  
android:inputType="textEmailSubject"  
android:inputType="textShortMessage"  
android:inputType="textLongMessage"  
android:inputType="textPersonName"  
android:inputType="textPostalAddress"  
android:inputType="textPassword"  
android:inputType="textVisiblePassword"  
android:inputType="textWebEditText"  
android:inputType="textFilter"  
android:inputType="textPhonetic" 
android:inputType="number"  
android:inputType="numberSigned"  
android:inputType="numberDecimal"  
android:inputType="phone"//拨号键盘  
android:inputType="datetime"  
android:inputType="date"//日期键盘  
android:inputType="time"//时间键盘

EditText默认是多行显示的,并且能够自动换行,即当一行显示不完的时候,他会自动换到第二行,可以设置==minLines==这个属性进行相应的限制。

另外很多时候我们可能要限制EditText只允许单行输入,而且不会滚动,比如上面的登陆界面的 例子,我们只需要设置

android:singleLine="true"

设置文字间隔,设置英文字母大写类型

android:textScaleX="1.5"    //设置字与字的水平间隔
android:textScaleY="1.5"    //设置字与字的垂直间隔
  • **sentences:**仅第一个字母大写
  • **words:**每一个单词首字母大小,用空格区分单词
  • **characters:**每一个英文字母都大写

实例演示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tips"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:layout_marginTop="15dp"
        android:text="@string/tips"
        android:gravity="center"
        >
    </TextView>

    <EditText
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:inputType="text"
        android:textSize="20sp"
        android:hint="@string/userName"
        android:layout_below="@id/tips"
        android:drawableLeft="@drawable/username"
        android:padding="10dp"
        android:drawablePadding="10dp"
        android:background="@drawable/edit_text"
        android:maxLines="1"
        >
    </EditText>
    <EditText
        android:id="@+id/pass_world"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:inputType="textPassword"
        android:textSize="16sp"
        android:hint="@string/passWorld"
        android:drawableLeft="@drawable/passworld"
        android:padding="10dp"
        android:drawablePadding="10dp"
        android:layout_below="@id/user_name"
        android:background="@drawable/edit_text"
        android:maxLines="1"
        >
    </EditText>
    <LinearLayout
        android:id="@+id/others"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/pass_world"
        >
        <TextView
            android:id="@+id/regin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/regin"
            android:textSize="24sp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"
            android:textColor="#cc3300"
            >
        </TextView>
        <TextView
            android:id="@+id/forget"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/forget"
            android:textSize="24sp"
            android:layout_marginBottom="15dp"
            android:textColor="#F70909"
            >
        </TextView>
    </LinearLayout>
    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:textSize="28sp"
        android:layout_below="@id/others"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/login"
        >
    </Button>
</RelativeLayout>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zuy0lZnE-1600624307043)(预习2.assets/image-20200921011649028.png)]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tips"
        android:textSize="24sp"
        android:textColor="#F70909"
        android:gravity="center"
        android:layout_marginTop="10dp"
        >
    </TextView>
    <LinearLayout
        android:id="@+id/phone_line"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/pthon_num"
            android:textSize="14sp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="5dp"
            >
        </TextView>
        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:layout_marginTop="20dp"
            android:inputType="phone"
            >
        </EditText>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="@string/sendcode"
            android:textSize="12sp"
            android:layout_marginTop="12dp"
            android:layout_marginRight="8dp">
        </Button>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/pass_world"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/phone_line"
        android:layout_marginRight="15dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码:"
            android:textSize="14sp"
            android:layout_marginLeft="15dp"
            >
        </TextView>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            >
        </EditText>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/pass_world2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/pass_world"
        android:layout_marginRight="15dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认密码:"
            android:textSize="14sp"
            android:layout_marginLeft="15dp"
            >
        </TextView>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            >
        </EditText>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/gender_line"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/pass_world2"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性         别:"
            android:textSize="14sp"
            >
        </TextView>
        <RadioGroup
            android:id="@+id/gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textSize="14sp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                >
            </RadioButton>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:textSize="14sp"
                >
            </RadioButton>
        </RadioGroup>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/likes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/gender_line"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="喜         好:"
            android:textSize="14sp"
            >
        </TextView>
        <CheckBox
            android:id="@+id/play_game"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打游戏"
            android:textSize="14sp"
            android:layout_marginLeft="15dp"
            >
        </CheckBox>
        <CheckBox
            android:id="@+id/play_balls"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打球"
            android:textSize="14sp"
            android:layout_marginLeft="15dp"
            >
        </CheckBox>
    </LinearLayout>
</RelativeLayout>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rDL5C4tZ-1600624307045)(预习2.assets/image-20200921011733734.png)]

Button组件

Button组件也是继承于TextView组件,这是一个按钮

按钮的常用样式演示

mian

<?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/btn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn1"
            android:textColor="#000"
            android:layout_margin="15dp"
            android:background="#00FF99"
            >
        </Button>

        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="@string/btn2"
            android:textColor="#fff"
            android:background="@drawable/btn2"
            >
        </Button>

        <Button
            android:id="@+id/btn3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="@string/btn3"
            android:textColor="#ff0033"
            android:background="@drawable/btn3"
                >
        </Button>

        <Button
            android:id="@+id/bnt4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="button4"
            android:background="@drawable/btn4"
            android:onClick="showToast"
            >
        </Button>

</LinearLayout>

btn2

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#C43C3C"
        >
    </solid>
    <corners
        android:radius="8dp"
        >
    </corners>
</shape>

btn3

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:color="#FFFF00"
        android:width="3dp"
        >
    </stroke>
    <corners
        android:radius="8dp"
        >
    </corners>

</shape>

btn4

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape>
            <solid
                android:color="#ffffcc"
                >
            </solid>
            <corners android:radius="5dp">
            </corners>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <solid
                android:color="#ff6600"
                >
            </solid>
            <corners android:radius="5dp">
            </corners>
        </shape>
    </item>
</selector>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XNq51qGx-1600624307048)(预习2.assets/image-20200921012210491.png)]

ImageButton组件

ImageButton 组件继承于 ImageView 组件

ImageButton图像按钮; 其实ImageButton和Button的用法基本类似,至于与图片相关的则和后面ImageView相同

消息提示框 Toast

直接上演示

<Button
    android:id="@+id/bnt4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:text="button4"
    android:background="@drawable/btn4"
    android:onClick="showToast"
    >
</Button>
package com.example.test4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public  void  showToast(View view){
        Toast.makeText(this,"我被点击了",Toast.LENGTH_SHORT).show();
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zRSFd0e7-1600624307050)(预习2.assets/image-20200921012807025.png)]

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pointer-faker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值