目录

一、TextView文本框

1、XML代码

2、结果展示

3、常见的属性

二、按钮

点击事件注册监听器的两种方式

1、普通按钮Button和图片按钮ImageButton

1.1、在XML文件中编写Button组件

1.2、为Button的点击事件注册一个监听器

1.3、结果展示

2、单选按钮RadioButton

2.1、在XML文件中编写RadioButton组件

2.2、Java代码

2.3、结果展示

3、复选按钮CheckBox

3.1、XML文件中添加复选按钮组件

3.2、Java代码

3.3、结果展示

4、小tips:android:textAllCaps大小写转换

三、EditText文本编辑框

1、基本语法格式

2、EditText中一些常用且有用的属性

3、获取编辑框中的内容


持续更新中~~~~~

一、TextView文本框

这算是Android页面中最简单的一个控件了吧,它主要的作用就是在页面上显示一段文本信息,就让我们简单的看下下面这个例子吧

1、XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content" android:text="This is Text View!"android:textSize="30dp" android:textColor="#00ff00"android:gravity="center"android:layout_gravity="bottom"/></LinearLayout>

2、结果展示

3、常见的属性

android:layout_width-->组件的宽度
    android:layout_height-->组件的高度
    android:text-->组件中的文本内容
    android:textSize-->文本的字体大小
    android:textColor--->文本的字体颜色
    android:gravity  -->组件中文本的对齐方式
    android:layout_gravity  --->组件在容器中的对齐方式

注意: android:layout_width与android:layout_height的属性值有wrap_content和match_parent

match_parent:表示控件的大小与父布局的大小一样

wrap_content:表示让当前控件的大小能够刚好包含住里面的内容

注意:android:layout_gravity与android:gravity的可选属性值有top、bottom、left、right、center等,可以用“|”来同时指定多个值,例如我们指定的center,他的效果相当于center_vertical|center_horizontal

当然,textview还有其他很多属性,我这里就不一一赘述了,感兴趣的可以自己查一下哦

textview也可以通过Java代码设置监听和获取其中的内容,可以参考下面的组件,这里就不作赘述了

二、按钮

按钮一般有三大要素:按钮组件、监听、触发后的具体操作,按钮的具体实现步骤也是根据这三个要素来的:(1)在res/layout目录下的XML文件中编写按钮组件(2)在Activity中为这个按钮组件添加监听器,并且在监听器中实现触发后的具体步骤

点击事件注册监听器的两种方式

方式一:

import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.yi);Button button1=(Button)findViewById(R.id.button1);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//点击控件时的逻辑}});}
}

方式二:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.yi);Button button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(this);}@Overridepublic void onClick(View v){switch(v.getId()){case R.id.button1://点击事件的逻辑代码break;default:break;}}}

采用哪种方式全凭你自己喜好了

1、普通按钮Button和图片按钮ImageButton

图片按钮与普通按钮的使用编写等都差不多,只不过图片按钮可以通过android:src来指定图片

1.1、在XML文件中编写Button组件

Button应该很熟悉了吧,它的出现频率可太高了,其实Button的可配置属性是和TextView是差不多的,我们还是来做个示例吧

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"><Buttonandroid:id="@+id/button_1"android:layout_width="match_parent"android:layout_height="wrap_content" android:text="Button_1"android:gravity="center"/></LinearLayout>

1.2、为Button的点击事件注册一个监听器

Java代码

public class ButtonActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_button); //引入布局Button button_1=(Button)findViewById(R.id.button_1);//设置监听button_1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//触发后的具体操作:点击按钮后在底部提示“you clicked button_1”Toast.makeText(ButtonActivity.this,"you clicked button_1",Toast.LENGTH_SHORT).show();}});}
}

1.3、结果展示

2、单选按钮RadioButton

2.1、在XML文件中编写RadioButton组件

单选按钮一般通过一个RadioGroup包裹多个RadioButton来实现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="学历"android:textSize="40dp" /><RadioGroupandroid:id="@+id/radiogroup_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/radio_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="大学"android:textSize="35dp" /><RadioButtonandroid:id="@+id/radio_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="研究生"android:textSize="35dp" /><RadioButtonandroid:id="@+id/radio_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="博士"android:textSize="35dp" /></RadioGroup><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="提交"android:textSize="30dp"android:layout_gravity="center_horizontal"/>
</LinearLayout>

2.2、Java代码

package com.example.button;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;public class RadioButtonActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_radio_button);//获取单选按钮组RadioGroup education=(RadioGroup)findViewById(R.id.radiogroup_1);//为单选按钮组添加监听器education.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int i) {//获取单选按钮RadioButton rb=(RadioButton)findViewById(i);Toast.makeText(RadioButtonActivity.this,"you click"+rb.getText(),Toast.LENGTH_SHORT).show();}});//获取提交按钮Button button_1=(Button)findViewById(R.id.button_1);//为提交按钮添加监听器button_1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//遍历单选框,找到被选择的选项for(int i=0;i<education.getChildCount();i++){RadioButton rb=(RadioButton)education.getChildAt(i);if(rb.isChecked()){Toast.makeText(RadioButtonActivity.this,"你选择了"+rb.getText(),Toast.LENGTH_SHORT).show();}}}});}
}

2.3、结果展示

3、复选按钮CheckBox

3.1、XML文件中添加复选按钮组件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="爱好"android:textSize="40dp"/><CheckBoxandroid:id="@+id/checkbox_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="音乐"android:textSize="35dp"/><CheckBoxandroid:id="@+id/checkbox_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="体育"android:textSize="35dp"/><CheckBoxandroid:id="@+id/checkbox_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="美术"android:textSize="35dp"/><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="提交"android:textSize="30dp"android:layout_gravity="center_horizontal"/>
</LinearLayout>

3.2、Java代码

package com.example.button;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;public class CheckBoxActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_check_box);//为复选框设置状态改变监听器CompoundButton.OnCheckedChangeListener checkbox_listener=new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {if (isChecked) {Toast.makeText(CheckBoxActivity.this, "you click" + compoundButton.getText().toString(), Toast.LENGTH_SHORT).show();}}};//获取CheckBox和ButtonCheckBox check1=(CheckBox)findViewById(R.id.checkbox_1);CheckBox check2=(CheckBox)findViewById(R.id.checkbox_2);CheckBox check3=(CheckBox)findViewById(R.id.checkbox_3);Button button=(Button)findViewById(R.id.button);//为CheckBox添加监听器check1.setOnCheckedChangeListener(checkbox_listener);check2.setOnCheckedChangeListener(checkbox_listener);check3.setOnCheckedChangeListener(checkbox_listener);//为Button设置监听器button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String hobby="";if(check1.isChecked()){hobby+=check1.getText().toString()+" ";}if(check2.isChecked()){hobby+=check2.getText().toString()+" ";}if(check3.isChecked()){hobby+=check3.getText().toString()+" ";}Toast.makeText(CheckBoxActivity.this, "你选择了" +hobby, Toast.LENGTH_SHORT).show();}});}
}

3.3、结果展示

4、小tips:android:textAllCaps大小写转换

我们通常可以看到按钮文本中,我们的设置的Button文字是Button_1,但显示的却是BUTTON_1,这是为什么呢?其实是系统会对Button中的所有英文字母进行自动大写转换,如果你不想要这样的效果,那么你可以添加这样一个属性来消除这样的效果:android:textAllCaps="false"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"><Buttonandroid:id="@+id/button_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button_1"android:gravity="center"android:textAllCaps="false"/></LinearLayout>

三、EditText文本编辑框

EditText主要是用于程序与用户之间的交互,它可以运行用户在这个控件里输入和编辑内容,并且程序也可以进行处理

1、基本语法格式

<EditText

属性

/>

2、EditText中一些常用且有用的属性

1、android:hint指定了一段提示性文本

android:hint="请输入姓名"

2、android:maxLines指定了EditText的最大行数,超过这个最大行数则文本向上滚动

android:maxLines="2"

3、android:inputType属性可以帮助输入框显示合适的类型

比如输入密码:android:inputType="textPassword"

3、获取编辑框中的内容

1、先获得编辑框组件

EditText et=(EditText)findViewById(R.id.edittext_1);

2、调用getText()方法即可得到编辑框中的内容

String text=et.getText().toString();

四、列表选择框Spinner

Spinner就是网页中常见的下来列表框,用于提供一系列的列表项供用户选择

1、基本格式

<Spinner

.......属性

/>

2、常用属性

Android中一些基础的UI组件相关推荐

  1. 【错误记录】Android 中使用 Kotlin 为 EditText 组件设置文本报错 ( Type mismatch. Required:Editable. Found:String )

    文章目录 一.报错信息 二.解决方案 一.报错信息 在 Android 中使用 Kotlin 开发 , EditText 组件如下 : 布局文件 : <EditTextandroid:id=&q ...

  2. Android中的基础控件TextView、Button、ImageView、EditText、ProgressBar

    文章目录 1 Android中的基础控件 1.1 控件的通用属性 2 TextView 2.1 TextView的继承关系 2.2 TextView的常用属性 3 EditText 3.1 常用属性 ...

  3. Android React Native使用原生UI组件

    Android React Native 已经将几个常用的原生组件进行了封装,比如 ScrollView 和 TextInput,但是并不是所有系统的原始组件都被封装了,因此有的时候我们不得不自己动手 ...

  4. Android(基本、高级UI组件)

    目录 一:前言 二:文本框组件 三:编辑框组件 四:按钮组件 4.1 匿名内部类监听器 4.2 onClick属性实现 4.3 图像按钮(Imagebutton) 4.4 单选按钮(radioButt ...

  5. android espresso跨程序,Android中使用Espresso进行UI测试

    在使用Android Studio创建项目时,Android Studio一般都会自动创建测试相关的包名和类,可见测试在Android Studio开发中具有很重要的地位了,但我却从来没有使用过. 今 ...

  6. Android中的基础控件CheckBox、RadioButton、ToggleButton、SeekBar

    文章目录 1 CheckBox 1.1 CheckBox介绍 2 RadioButton 2.1 RadioButton介绍 3 ToggleButton 3.1 ToggleButton介绍 4 S ...

  7. SAP中BOM基础数量及组件数量单位比例关系的注意事项

    下图是BOM展开功能CS11在正式系统和测试系统的截图.从截图中的对比不难看出,最下级的原材料A20981-110在组件的数量为1,实际按BOM中的设定比例折算,应该是1个成品,对应需要0.125件原 ...

  8. 在已有vue项目中半途引入cube ui组件库的使用遇到的坑(血泪)

    直接进入正题(我这儿是属于cube-ui普通编译) 1,在你的vue项目中找到package.json文件安装cube-ui 终端输入命令   npm install cube-ui --save 2 ...

  9. Android中所有UI组件基类是,【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity...

    问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...

最新文章

  1. 自己动手搭建Git服务器-SCM-Manager
  2. 计算机网络总结:第五章 链路层
  3. python array按行归一化_机器学习 第40集:特征不归一化有什么危害?特征归一化公式是什么?( 含有笔记、代码、注释 )...
  4. js设置和清除cookie
  5. MYSQL中最基础的的聚合函数(avg求平均值及count求和)
  6. Kaggle 发布首份数据科学从业报告 | 不及美国同行1/3,中国数据科学家平均年薪约3万美元
  7. LeetCode 1271. 十六进制魔术数字(进制转换)
  8. 某云商城发卡网源码 带视频教程
  9. 后台创建窗体下拉列表
  10. 马斯克一说特斯拉“全自动驾驶”,大家就想笑
  11. C语言基础专题 - 预处理
  12. 面试总结-接口测试面试题
  13. MFC制作的入坑级别管理系统
  14. 普中51单片机的贪吃蛇教程
  15. 数据可视化实验:python数据可视化-柱状图,条形图,直方图,饼图,棒图,散点图,气泡图,雷达图,箱线图,折线图
  16. 黄金时代 —— Pytorch学习记录(一)
  17. linux modprobe命令参数
  18. Linux实用的快捷键
  19. 电脑学习编程or使用笔记本
  20. html怎么把字做成动画效果,利用纯CSS实现动态的文字效果实例

热门文章

  1. Opencv——信息信用卡识别
  2. 超详细的springBoot学习笔记
  3. 超级详细全面的LBP特征(Local Binary Pattern)讲解
  4. Unity用脚本配置简单的数字艺术字体CustomFont
  5. 计算机 二进制与数字媒体是什么关系,数字媒体
  6. ESP32连接DS3231实时时钟(RTC)的方法
  7. pytorch 数据放到GPU上面
  8. Python创建用户并随机生成8位密码
  9. Markdown语法写博客
  10. 普歌-码上鸿鹄团队:配合node+MongoDB后台+vue-cli用API接口获取数据库数据