android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件
主要参考《第一行代码》
1.TextView:
功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息
如:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#0000ff" android:textSize="40sp" android:text="@string/hello_world" />
显示效果:
上面的xml代码中,设置了几个常用的属性:
android:layout_width和android:layout_height分别设置控件的宽高
textColor设置显示的文本的颜色
textSize设置显示的文本的字体大小
text设置显示的文本内容。
2.Button:
前面用到的比较多,经常被用到的就是通过id获取按钮,然后绑定单击监听事件,这里仅列举个例子:
activity_main.xml:
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#0000ff" android:textSize="40sp" android:text="@string/hello_world" /> <Button android:id="@+id/btn" android:layout_below="@id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnText"/>
MainActivity.java:
public class MainActivity extends ActionBarActivity { private TextView tv; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub tv.setText("It's changed!"); } }); } }
3.EditText:
即文本输入框,如下修改程序,在按钮之上添加一个EditText,点击按钮,会获取EditText的值并把它设置为TextView的Text属性:
activity_main.xml:
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#0000ff" android:textSize="40sp" android:text="@string/hello_world" /> <EditText android:id="@+id/et" android:layout_below="@id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/hintText" /> <Button android:id="@+id/btn" android:layout_below="@id/et" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnText"/>
MainActivity.java:
public class MainActivity extends ActionBarActivity { private TextView tv; private Button btn; private EditText et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); et = (EditText)findViewById(R.id.et); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Editable text = et.getText(); tv.setText(text.toString()); } }); } }
运行效果:
输入值,然后点击按钮:
注意到由于EditText的layout_height属性是wrap_content,所以会随着输入内容的增多不断变大,影响整体布局。若想固定其高度,可以设置maxLines属性,设置最多只显示的行数,其他内容向上滚动
如:android:maxLines = “1”
EditText的高度就不会变化了。
4.ImageView:
使用来显示图片的一个控件,之前的程序中曾经用到过,当然,它最主要的属性肯定是要显示图片的来源了,即android:src属性,将要显示的图片存放在res/drawable中,如图片名为hero.png。要显示该图片,设置android:src=”@drawable/hero”即可。
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/hero"/>
显示结果:
5.ProgressBar:
即进度条,使用style属性,可以设置不同的显示风格:
1)不设置style属性或者设置为style="?android:attr/progressBarStyle" ,环形显示
2)style="?android:attr/progressBarStyleHorizontal",水平横条显示
3)style="?android:attr/progressBarStyleLarge",大号的环形显示
4)style="?android:attr/progressBarStyleSmall",小号的
进度条当然是用来显示进度的,通过findViewById()获取ProgressBar,然后使用setProgress()就可以设置当前进度,使用getProgress()可以获取当前进度。
如:
布局代码:
<ProgressBar android:id="@+id/pb" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" /> <Button android:id="@+id/btn" android:layout_below="@id/pb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add_progress"/>
Activity代码:
public class MainActivity extends ActionBarActivity { private ProgressBar pb; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pb = (ProgressBar) findViewById(R.id.pb); btn = (Button) findViewById(R.id.btn); Log.i("PB",pb.getProgress()+""); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("PB",pb.getProgress()+""); pb.setProgress(pb.getProgress()+10); } }); } }
运行结果:
初始时,默认进度为0
多次点击按钮之后:
达到android:max所设置的最大值后,再加也不会有变化了。
6.AlertDialog:
这个控件就是弹出一个对话框,类似于桌面开发中的模态对话框,必须关闭该对话框,才能进行后续交互操作,可用于显示比较重要的内容。
AlertDialog的构造方法都是protected,没法直接通过构造来创建AlertDialog,但是可以通过其内部类Builder来创建。
具体使用可以参考帮助手册中关于这个内部类的帮助信息,下面举个简单例子:
AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("Warning"); dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); dialog.setMessage("warning, hahaha"); dialog.show();
运行结果:
7.ProgressDialog:
类似于AlertDialog,也是对话框,不过它显示的内容是一个进度条,好像是对话框和进度条两个控件的结合。
ProgressDialog pd = new ProgressDialog(this); pd.setTitle("Data Loading..."); pd.show();
运行结果:
android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件的更多相关文章
- android菜鸟学习笔记14----Android控件(三) ListView的简单使用
MVC模式: MVC的基本原理就是通过Controller连接View和Model.当View中所显示的数据发生变化时,会通知Controller,然后由Controller调用Model中的相关方法 ...
- android菜鸟学习笔记30----Android使用百度地图API(一)准备工作及在应用中显示地图
1.准备工作: 百度地图API是免费开放的,但是需要申请API Key: 1)先注册一个百度开发者帐号 2)进入百度开放服务平台http://developer.baidu.com/ 3)进入LBS云 ...
- android菜鸟学习笔记7----android布局(二)
3.FrameLayout:帧布局 如同Flash或者photoshop中图层的概念,在上面的图层遮盖下面的图层,没被遮到的地方仍然显示出来. 右击res/layout,然后在弹出的菜单中选择new, ...
- android菜鸟学习笔记31----Android使用百度地图API(二)获取地理位置及地图控制器的简单使用
1.获取当前地理位置: Android中提供了一个LocationManager的类,用于管理地理位置.不能通过构造函数获取该类的实例,而是通过Context的getSystemService(): ...
- android菜鸟学习笔记21----ContentProvider(一)ContentProvider的简单使用
ContentProvider是Android四大组件之一,它用来封装数据,并通过ContentResolver接口将数据提供给其他应用.只有当需要在多个应用之间共享数据时才会用到ContentPro ...
- android菜鸟学习笔记8----Activity(一)
Activity是android应用程序中重要的组件之一,常听到的android四大组件是Activity.Service.BroadcastReceiver和ContentProvider.它间接继 ...
- android菜鸟学习笔记29----Android应用向用户发送提示信息的方式总结
常见的向用户发送提示信息的方式有3种,分别为: 1)发送Toast信息 2)弹出对话框 3)发送通知 总结如下: 方式1:发送Toast信息: 这种方式最简单,在之前的学习中多次使用过.Toast是在 ...
- android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据
主要是基于HTTP协议与服务端进行交互. 涉及到的类和接口有:URL.HttpURLConnection.HttpClient等 URL: 使用一个String类型的url构造一个URL对象,如: U ...
- android菜鸟学习笔记2----关于adb
adb : android debug bridge android调试桥 路径:adt-bundle目录/sdk/platform-tools/adb.exe 常见的adb命令: adb devic ...
随机推荐
- Guice 4.1教程
Guice是Google开发的一个开源轻量级的依赖注入框架,运行速度快,使用简单. 项目地址:https://github.com/google/guice/ 最新的版本是4.1,本文基于此版本. 0 ...
- BZOJ3631 松鼠的新家(树链剖分)
题目链接 松鼠的新家 差不多可以说是树链剖分的模板题了,直接维护即可. #include <bits/stdc++.h> using namespace std; #define REP( ...
- luogu P1314 聪明的质监员
题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: 1 .给定m 个区间[L ...
- Hdoj 2509 Be the Winner
Diciption Let's consider m apples divided into n groups. Each group contains no more than 100 apples ...
- Jave工具——servlet+jsp编程中mysql数据库连接及操作通用工具类
该工具类是在JavaWeb中连接mysql所用到的通用工具类 该类用于Java+Servlet的编程中,方便数据库的操作,连接,获取其列表值.下面是这个数据库操作类的通用方法,基本上能够用于类里面只含 ...
- 全栈一路坑(4)——创建博客的API
上一篇博客:全站之路一路坑(3)——使用百度站长工具提交站点地图 这一篇要搭建一个API平台,一是为了给博客补充一些功能,二是为以后做APP提供数据接口. 首先需要安装Django REST Fram ...
- AbstractFactory
定义:为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类. (1)定义产品接口 /** * 第一种系列的产品 * @author Administrator * */ interfa ...
- ListView设置某一项item的文本居中
使用ListView和volley写了一个使用网络获取天气的demo ListView中Item的文本模式都是左侧对齐 我这边需要一些标题文本居中对齐 网上也找不到示例,不过找到了getView这个函 ...
- EV根证书
Extended Validation SSL Certificates翻译为扩展验证型服务器证书(EV 服务器证书 或 EV SSL证书),申请该证书需要经过最彻底的身份验证,确保证书持有组织的真实 ...
- oracle行锁select for update
oracle行锁select for update 学习了:https://blog.csdn.net/zdwzzu2006/article/details/50490157 学习了:https:// ...