Android中的几个基本常用控件
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中的几个基本常用控件的更多相关文章
- Android音乐、视频类APP常用控件:DraggablePanel(2)
Android音乐.视频类APP常用控件:DraggablePanel(2) 附录文章1主要演示了如何使用DraggablePanel 的DraggableView.DraggablePanel ...
- Android音乐、视频类APP常用控件:DraggablePanel(1)
Android音乐.视频类APP常用控件:DraggablePanel(1) Android的音乐视频类APP开发中,常涉及到用户拖曳视频.音乐播放器产生一定交互响应的设计需求,最典型的以You ...
- Android中Chronometer 计时器和震动服务控件
Chronometer 计时器控件 首先在布局文件中添加chronometer控件:然后在mainActivity中获取到该控件 4 然后通过Button时间监听器中开启计时操作 5 chronome ...
- Android中相对布局的两个控件
<Button android:id="@+id/button3" android:layout_width="wrap_content" android ...
- Android support library支持包常用控件介绍(一)
谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现Material Design设计效果,官方给出了Android support design library 支 ...
- Android中常用控件及属性
在之前的博客为大家带来了很多关于Android和jsp的介绍,本篇将为大家带来,关于Andriod中常用控件及属性的使用方法,目的方便大家遗忘时,及时复习参考.好了废话不多讲,现在开始我们本篇内容的介 ...
- 五、Android学习第四天补充——Android的常用控件(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...
- android xml 常用控件介绍
android常用控件介绍 ------文本框(TextView) ------列表(ListView) ------提示(Toast) ------编辑框(EditText) ...
- Android support library支持包常用控件介绍(二)
谷歌官方推出Material Design 设计理念已经有段时间了,为支持更方便的实现 Material Design设计效果,官方给出了Android support design library ...
随机推荐
- C# 利用反射动态给模型Model 赋值
https://www.cnblogs.com/waitingfor/articles/2220669.html object ff = Activator.CreateInstance(tt, nu ...
- [ZOJ 4014] Pretty Matrix
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5742 AC代码: /* * 反思: * 1.遇到简单题别激动,先把它 ...
- 【Git】Git使用记录: remove *.lock eg: index.lock/head.lock
问题 Another git process seems to be running in this repository, e.g. an editor opened by 'git commit' ...
- 区别samtools faid产生的.fai文件功能和bwa index 产生的四个文件的功能
samtools faidx 能够对fasta 序列建立一个后缀为.fai 的文件,根据这个.fai 文件和原始的fastsa文件, 能够快速的提取任意区域的序列 用法: samtools faidx ...
- mysql 插入中文字段报错 "Incorrect string value: '\\xE6\\xB5\\x8B\\xE8\\xAF\\x95...' for column 'title' at row 1"
1. 查看一个 database 或一个 table 的编码show create database mytestdb;show create table testapp_article; mysql ...
- 拿取页面值 跟拿取value里面的值
拿取页面输入框的数值 使用 val() val()设置或返回表单字段的值 拿取value里面的数值 value(); attr() 获取属性值
- heap_sort
(from wikipedia) 构建步骤: 建成一个大顶堆 第一个元素依次和最后一个元素交换,由于交换后新的堆顶元素可能违反大根堆的性质,因此需要对当前无序区(1,2,...,n-1)调整为新堆 不 ...
- web优化及web安全攻防学习总结
web优化 前端:(高性能网站建设进阶指南) 使用gzip压缩(节省服务器的 网络带宽) 减少http请求( 减少DNS请求所耗费的时间. 减少服务器压力. 减少http请求头) 使用cdn(用户可以 ...
- 自定义 Cordova插件(基础篇)
cordova自定义插件 注意:存放自定义cordova插件目录不能有空格可能会报错 cordova的安装 下载node.js,安装完成后你可以在命令行中使用node和npm. 安装cordova使用 ...
- C# 指定程序打开指定文件
Process process = new Process(); process.StartInfo.FileName = 指定程序exe文件路径: process.StartInfo.Argumen ...