Android 中常用的几大UI控件

1. TextView : 用于在界面中显示一段文本信息
<TextView
android:id="@+id/text_view" //给这个textview定义一个唯一标识符
android:layout_width="match_parent"
android:layout_height="wrap_parent" //所有的控件都有layout_width和layout_height属性
android:text="This is a text"
android:gravity="center" //修改文字的对齐方式(默认为左对齐).top.bottom,left,right,center等
/>

其他属性:android:textSize(单位为sp), android:textColor等 (了解更多属性,可查阅文档)

2. Button
activity_main.xml中:
<Button
android:id="@_+id/button1"
android:layout_height="wrap_parent"
android:layout_width="match_parent"
android:text="Button"
android:textAllCaps="false"
/>

Main_activity.java中:
Button b1 = (Button)findViewById(R.id.button1)
b1.setOnClickListener(new View.onClickListener()){
@Override
public void onClick(View v){
}
}

3. EditText:允许用户在EditText中输入和编辑内容,并可以在程序中对这些内容(findViewById(),getText())进行处理。
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

其他常用属性:android:hint用于指定一段提示性文本

4. ImageView:用于在UI中展示图片
<ImageView
android:id = "@+id/image_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:src="@drawable/image_1"
/>
如果想在程序中修改image,可使用setImageResource(R.drawable.image_2)方法

5.ProgressBar:用于在UI显示一个进度条
getVisibility()
setVisibility()
值:View.VISIBLE View.INVISIBLE View.GONE
View.INVISIBLE与View.GONE的区别:
都是进度条不可见,但View.INVISIBLE表示控件仍然存在,仍然占据着屏幕的空间,相当于透明;
View.GONE表示控件不再占用任何屏幕控件

6. AlertDialog: above the UI layout,屏蔽其他控件的交互能力.
用于显示一些很重要的信息,比如用户删除一些重要数据时出现。
AlertDialog不需要在layout中定义,只需要在java文件中声明。
举例如下:当用户点击按钮时,会弹出一个AlertDialog

public void onClick(View view) {
//定义一个AlertDialog的实例
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Alert");
alertDialog.setMessage("This is an important Msg");
//是否可以用Back键关闭对话框
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});
alertDialog.show();
}
});

7. ProgressDialog: 与AlertDialog相似,above the UI, 屏蔽其他控件的交互。
但ProgressDialog会在对话框中显示一个进度条,用于表示当前操作比较耗时,让用户耐心等待。
ProgressDialog.Builder progressDialog = new ProgressDialog.Builder(MainActivity.this);
progressDialog.setTitle("ProgressDialog");
progressDialog.setMessage("This is a progressDialog");
progressDialog.setCancelable(true);
progressDialog.show();
注意,如果在setCancelable() 中传入了false ,表示ProgressDialog是不能通过Back键取消掉的,
这时你就一定要在代码中做好控制,当数据加载完成后必须要调用ProgressDialog的dismiss() 方法来关闭对话框,
否则ProgressDialog将会一直存在。

Android中的几个基本常用控件的更多相关文章

  1. Android音乐、视频类APP常用控件:DraggablePanel(2)

     Android音乐.视频类APP常用控件:DraggablePanel(2) 附录文章1主要演示了如何使用DraggablePanel 的DraggableView.DraggablePanel ...

  2. Android音乐、视频类APP常用控件:DraggablePanel(1)

     Android音乐.视频类APP常用控件:DraggablePanel(1) Android的音乐视频类APP开发中,常涉及到用户拖曳视频.音乐播放器产生一定交互响应的设计需求,最典型的以You ...

  3. Android中Chronometer 计时器和震动服务控件

    Chronometer 计时器控件 首先在布局文件中添加chronometer控件:然后在mainActivity中获取到该控件 4 然后通过Button时间监听器中开启计时操作 5 chronome ...

  4. Android中相对布局的两个控件

    <Button android:id="@+id/button3" android:layout_width="wrap_content" android ...

  5. Android support library支持包常用控件介绍(一)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现Material Design设计效果,官方给出了Android support design library 支 ...

  6. Android中常用控件及属性

    在之前的博客为大家带来了很多关于Android和jsp的介绍,本篇将为大家带来,关于Andriod中常用控件及属性的使用方法,目的方便大家遗忘时,及时复习参考.好了废话不多讲,现在开始我们本篇内容的介 ...

  7. 五、Android学习第四天补充——Android的常用控件(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...

  8. android xml 常用控件介绍

    android常用控件介绍 ------文本框(TextView)     ------列表(ListView)     ------提示(Toast)     ------编辑框(EditText) ...

  9. Android support library支持包常用控件介绍(二)

    谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...

随机推荐

  1. [IoC容器Unity]第三回:依赖注入

    1.引言 上节介绍了,Unity的Lifetime Managers生命周期,Unity具体实现依赖注入包含构造函数注入.属性注入.方法注入,所谓注入相当赋值,下面一个一个来介绍. 2.构造函数注入 ...

  2. React-native完整配置流程

    开头敲黑板!! 无论你是RN的新手还是老手,跟着流程走,RN项目搭建起来完全不是问题!   一.网址收集 expo配置网址:https://blog.expo.io/building-a-react- ...

  3. js var 以及 let 的差异

    例子 window.checklist=[{"boardname":"motor_board","cur":"1.0.0" ...

  4. Redis 持久化RDB 和AOF

    一.持久化之全量写入:RDB rdb配置 [redis@6381]$ more redis.conf save 900 1 save 300 10 save 60 10000 dbfilename & ...

  5. Keras + LSTM 做回归demo 2

    接上回, 这次做了一个多元回归 这里贴一下代码 import numpy as np np.random.seed(1337) from sklearn.model_selection import ...

  6. [转]OpenStreetMap/Google/百度/Bing瓦片地图服务(TMS)

    转自:https://blog.csdn.net/youngkingyj/article/details/23365849 开源与成熟商业的瓦片地图服务(TMS  2  WMTS),都有如下共同的特性 ...

  7. The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration

    用过Mysql的人都知道,这个时区问题真个磨人的小妖精,哪天一忘记设置了就会出来磨磨你!!! 之前用的解决方法都是在Mysql的配置上添加与时区相关的配置,但是今天看到一篇博客:https://blo ...

  8. Kafka集群安装部署、Kafka生产者、Kafka消费者

    Storm上游数据源之Kakfa 目标: 理解Storm消费的数据来源.理解JMS规范.理解Kafka核心组件.掌握Kakfa生产者API.掌握Kafka消费者API.对流式计算的生态环境有深入的了解 ...

  9. VMware虚拟机安装CentOS 6.9图文教程

    1.Win7安装VMware虚拟机比较简单,直接从官网下载VMware安装即可,这里不再叙述. 2.接着从CentOS官网直接下载CentOS 6.9的iso镜像文件. 3.打开VMware,点击创建 ...

  10. Matlab - 各种函数学习

    一.find函数 (1)find()函数的基本功能是返回向量或者矩阵中不为0的元素的位置索引. X = [1 0 4 -3 0 0 0 8 6]; index = find(X) 返回不为零的位置索引 ...