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. linux下配置zookeeper注册中心及运行dubbo服务

    dubbo和zookeeper的关系 简单来说打个比方:dubbo就是动物园的动物,zookeeper是动物园.如果游客想看动物的话那么就去动物园看.比如你要看老虎,那么动物园有你才能看到.换句话说我 ...

  2. 在Heroku上免费部署ASP.NET Core(使用Docker和CircleCI)

    创建 ASP.NET Core应用 使用命令行即可创建一个模板项目 dotnet new webapi 完整代码 https://github.com/Ibro/AspNetCoreHerokuDoc ...

  3. .NET in Browser - Blazor

    什么是Blazor Blazor 是一个实验性的. NET web 框架, 使用 C# 和 HTML 在任何浏览器中不需要插件即可运行 WebAssembly 程序集. 什么是WebAssembly ...

  4. Lintcode93-Balanced Binary Tree-Easy

    93. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  5. ELK6.6.0+filebeat6.6.0部署

    elastic不能用root用户去启动,否则会报错,所以创建elastic用户ES集群部署 1.创建elastic用户 $ useradd elastic $ passwd elastic 2..部署 ...

  6. 历届试题 小数第n位 (求循环节)

    只要被除数出现重复,就表明循环节出现了.即使商不是循环小数,也可以补0作为循环节,这样就可以统一处理了. AC代码 #include <stdio.h> #include <vect ...

  7. js三元表达式

    条件 ? true的时候执行 : false时候执行 const x = 20; let answer; if (x > 10) { answer = 'greater than 10'; } ...

  8. scrapy数据存储在mysql数据库的两种方式

    方法一:同步操作 1.pipelines.py文件(处理数据的python文件) import pymysql class LvyouPipeline(object): def __init__(se ...

  9. numpy 数组索引数组

    在numpy中,数组除了可以被整数索引,还可以被数组索引. a[b]就是已数组b的元素为索引,读取数组a的值. 当被索引数组a是一维数组,b是一维或则多维数组时,结果维度维度与索引数组b相同. a = ...

  10. switch与if语句的应用

    C语言自学之switch与if语句的应用 #include<stdio.h> #include<stdlib.h> int main() { ;//需要计算的年份 ;//需要计 ...