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. docker笔记(2)-----容器连接

    2019-01-12  13:57:36 Dockerfile基本结构: 基础镜像内容:FROM指明base image 维护者信息: 镜像操作指令:RUN指令,每运行一条RUN指令,镜像添加新的一层 ...

  2. Spark Standalone cluster try

    Spark Standalone cluster node*-- stop firewalldsystemctl stop firewalldsystemctl disable firewalld-- ...

  3. Git复习步骤

    1.首先肯定是安装与配置了 首先要下载Git,然后设置用户名/邮箱 https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c ...

  4. git命令合并分支代码

    对于复杂的系统,我们可能要开好几个分支来开发,那么怎样使用git合并分支呢? 合并步骤:1.进入要合并的分支(如开发分支合并到master,则进入master目录)git checkout maste ...

  5. jquary高级和ajax

    jquary高级: 1.动画 1.三种方式显示与隐藏元素 1.默认显示和隐藏的方式 1.show([speed],[easing],[fn]):显示 [speed],[easing],[fn] spe ...

  6. java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context 错误

    spring boot 项目启动报错:原因一般是注入了相同名字的service -- :: com.gxcards.mes.MainWwwWeb: logStartupProfileInfo INFO ...

  7. Linux系统优化及基础命令

    1.Linux系统优化及基础命令 2. vim编辑器 vim 操作命令 在命令模式下操作 pageup 往上翻页(重要指数****)pagedown 往下翻页(重要指数****)H 移动到屏幕首行gg ...

  8. mysql Access denied for user root @localhost (using password:YES)错误

    C:\AppServ\MySQL> mysql -u root -p Enter password:  ERROR 1045 (28000): Access denied for user 'r ...

  9. 一个axios的简单教程

    转载于:https://www.jianshu.com/p/13cf01cdb81f 转载仅供个人学习 首先要明白的是axios是什么:axios是基于promise(诺言)用于浏览器和node.js ...

  10. CodeIgniter框架解析

    转载于:https://www.cnblogs.com/xiaoxiaoqingyi/p/6901654.html 转载仅为以后自己学习. 业余花了点时间看看CodeIgniter框架(简称CI),C ...