Android基本控件和Activity的基本应用

一.Android基本控件

1.基本控件概述

TextView 显示文字,相当于Panel

ImageView 显示图片

EditText 输入框,可编辑,可设置软键盘方式

Button 按钮,可附带图片

CheckBox 复选框

RadioButton 单选按钮(和 RadioGroup 配合使用)

2.按用途分类

文本控件

– TextView

– EditText

按钮控件

– Button

– ImageButton

状态开关按钮

– ToggleButton

单选与复选按钮

– CheckBox和RadioButton

图片控件

– ImageView

3.常用控件介绍

1)TextView

定义:文本内容显示控件

常用属性:

text 显示内容属性

textSize 显示内容的字体大小,单位为sp

textcolor 显示内容的字体颜色 格式为"#RGB"

textStyle 显示字体样式

XML代码如下:

<TextView

//控件id

android:id = “@+id/xxx” ( @+id/xxx表示新增控件命名为xxx)

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center_vertical”

android:gravity=“center”

//文本文字

android:text=“性别”

//背景颜色

android:textColor="@android:color/black"

//文本大小

android:textSize=“20sp”/>**

2)Button Button组件是一个简单组件按钮,Button是TextView的子类,所以TextView

有的属性Button都继承了

定义:文本按钮控件,可设置带图标的文本按钮

XML代码:

< Button

android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="登录"
android:textSize="22sp" />
复制代码

java代码: //获取按钮控件

btnlogin = findViewById(R.id.btn_login);

//2.监听button按钮的点击事件

btnlogin.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

//3.处理按钮的点击事件

//3.1 获取用户名和密码的值

String username = etUsername.getText().toString().trim();

String password = etPassword.getText().toString();

//3.2比较用户名和密码是否正确,然后给出提示

if (username.equals(“cc”)&&password.equals(“123456”)){

Toast.makeText(MainActivity.this,“登录成功”,Toast.LENGTH_LONG).show();

}else {

Toast.makeText(MainActivity.this,“用户名或密码不对”,Toast.LENGTH_LONG).show();

}

}

});

btnlogin.setOnClickListener(this);

}

3)EditText EditText和Button一样是TextView的子类

定义:文本内容编辑控件

XML代码如下:

4)CheckBox 父类是CompoundButton,而CompoundButton的父类是Button

定义:复选框控件(只有两种状态的按钮)

属性:

checked=“false|true”

android:button="@null" 可以隐藏掉前面的小圆点

Button的所有属性

点击事件:

RadioGroup.OnCheckedChangeListener

onCheckedChanged(RadioGroup, int checkedId)

5)RadioGroup和RadioButton

RadioButton和CheckBox一样父类是CompoundButton

1.定义: 单选项控件,一般在RadioGroup布局中使用 出现在RadioGroup布局中才可以实现单选

2.点击事件:

RadioGroup.OnCheckedChangeListener

onCheckedChanged(RadioGroup, int checkedId)

checkedId:被点击的RadioButton的ID

XML代码如下:、

<RadioButton
    android:id="@+id/flowers_xiuqiuhua"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="绣球花"
    android:textSize="20sp" />

<RadioButton
    android:id="@+id/flowers_yulanhua"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="玉兰花"
    android:textSize="20sp" />

<RadioButton
    android:id="@+id/flowers_mudanhua"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="牡丹花"
    android:textSize="20sp" /></RadioGroup>
复制代码

二.Activity的基本应用

*活动

*Android组件中最基本也是最常见的组件

*在Android应用中,一个Activity通常就是一个单独的屏幕

*每个Activity都被实现为一个从活动积累继承来的独立的类

*活动类显示的用户接口由视图控件组成,并对事件做出响应

Activity的生命周期

Activity的生命周期很重要

主要包含六个方法:

onCreate,onStart,onResume,onPause,onStop,onDestory

onCreate函数: 注册你要用到的变量,比如说service,receiver,这些变量是无论你的Activity是在前台还是在后台都能够被响应到的,然后调用上面那个用来初始化的函数初始化布局信息。

onStart函数: 注册一些变量,这些变量必须在Activity在前台的时候才能够被响应。

onResume函数: 调用一些刷新UI的函数,每当Activity调用到这里时,就要刷新一下UI各控件的状态。

onPause函数: 一般是做一些变量的设置,因为这个时候Activity马上就要切到后台处理,可能有些变量就要被释放掉或者状态要做些相应的调整

onStop函数: 反注册在onStart函数中注册的变量。

onDestory函数: 反注册在onDestory函数中注册的变量。

转载于:https://juejin.im/post/5cb1ae5ff265da0393785966

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值