Android开发03—Android常用基本控件(上)
1. 文本控件介绍
1) TextView类
TextView类继承自View类。TextView控件的功能是向用户显示文本内容,同时可选择性的让用户编辑文本。其自身被设置为不可编辑,其子类EditText被设置为可以编辑。
2) EditText类
EditText继承自TextView。EditText与TextView最大的不同就是用户可以对EditText控件进行编辑,同时还可以为EditText设置监听器,用来检测用户输入是否合法。
实例1:接收用户输入的电子邮箱地址和电话号码:
color.xml
string.xml
main.xml
MyAndroidTest.java
2. 按钮控件
1) Button类简介
Button继承自TextView类,用户可以对Button控件执行按下或单击操作。Button控件的用法简单,主要是为Button控件设置View.OnClickListener监听器并在监听器的实现代码中开发按钮按下的事件。
一般格式:
2) ImageButton类
ImageButton类继承自ImageView类,ImageButton控件与Button控件的主要区别在于,ImageButton类没有text属性,即按钮显示图片而不是文本。ImageButton控件设置按钮显示的图片通过android:src属性,也可以通过setImageResource(int)方法来实现。
默认情况下,ImageButton同Button一样具有背景色,当按钮处于不同状态喜爱时,背景色也会变化。当ImageButton所显示图片不能完全覆盖掉背景色时,这种显示效果会相当恶心,所以使用ImageButton一般要将背景色设置为其他图片或直接设置为透明。
注意:新建xml文件文件名不能是大写字母
实例2:为ImageButton按钮控件的不同状态设置不同的图片。
color.xml:
myselector.xml:
main.xml:
3. 状态开关
1) ToggleButton类
ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的文本。
android:Off 未被选中时显示的文本
android:On 选中时显示的文本
实例3:ToggleButton的用法:
strings.xml:
main.xml
MyAnroidTest.java:
package qijia.si;
1. 文本控件介绍
1) TextView类
TextView类继承自View类。TextView控件的功能是向用户显示文本内容,同时可选择性的让用户编辑文本。其自身被设置为不可编辑,其子类EditText被设置为可以编辑。
2) EditText类
EditText继承自TextView。EditText与TextView最大的不同就是用户可以对EditText控件进行编辑,同时还可以为EditText设置监听器,用来检测用户输入是否合法。
实例1:接收用户输入的电子邮箱地址和电话号码:
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="shadow">#fd8d8d</color> </resources>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, AndroidTest!</string> <string name="app_name">AndroidTest</string> <string name="tvEmail">邮箱地址</string> <string name="etEmail">请输入电子邮件地址</string> <string name="tvPhone">电话号码</string> <string name="etPhone">请输入电话号码</string> <string name="etInfo">此处显示登记信息</string> </resources>
main.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:shrinkColumns="0,2" android:layout_width="fill_parent" android:id="@+id/tableLayout1" android:layout_height="fill_parent"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tvEmail" android:ellipsize="end" android:autoLink="email" android:id="@+id/tvEmail"></TextView> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:selectAllOnFocus="true" android:hint="@string/etEmail" android:id="@+id/etEmail"></EditText> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tvPhone" android:ellipsize="middle" android:autoLink="phone" android:id="@+id/tvPhone"></TextView> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/etPhone" android:selectAllOnFocus="true" android:maxWidth="160px" android:phoneNumber="true" android:singleLine="true" android:id="@+id/etPhone"></EditText> </TableRow> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="EditText" android:editable="false" android:hint="@string/etInfo" android:cursorVisible="false" android:lines="5" android:shadowColor="@color/shadow" android:shadowDx="2.5" android:shadowDy="2.5" android:shadowRadius="5.0"></EditText> </TableLayout>
MyAndroidTest.java
package qijia.si; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.EditText; public class AndroidTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); EditText etEmail = (EditText) findViewById(R.id.etEmail); etEmail.setOnKeyListener(myOnKeyListener); } private OnKeyListener myOnKeyListener = new OnKeyListener(){ public boolean onKey(View v,int keyCode, KeyEvent event){ EditText etInfo = (EditText) findViewById(R.id.editText3); EditText etEmail = (EditText) findViewById(R.id.etEmail); etInfo.setText("您输入的邮箱地址是:"+etEmail.getText()); return true; } }; }
2. 按钮控件
1) Button类简介
Button继承自TextView类,用户可以对Button控件执行按下或单击操作。Button控件的用法简单,主要是为Button控件设置View.OnClickListener监听器并在监听器的实现代码中开发按钮按下的事件。
一般格式:
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.Id.ID); btn.setOnClickListener(new OnClickListener){ public void onClick(View v){ //处理的事件 } }; }
2) ImageButton类
ImageButton类继承自ImageView类,ImageButton控件与Button控件的主要区别在于,ImageButton类没有text属性,即按钮显示图片而不是文本。ImageButton控件设置按钮显示的图片通过android:src属性,也可以通过setImageResource(int)方法来实现。
默认情况下,ImageButton同Button一样具有背景色,当按钮处于不同状态喜爱时,背景色也会变化。当ImageButton所显示图片不能完全覆盖掉背景色时,这种显示效果会相当恶心,所以使用ImageButton一般要将背景色设置为其他图片或直接设置为透明。
注意:新建xml文件文件名不能是大写字母
实例2:为ImageButton按钮控件的不同状态设置不同的图片。
color.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="back">#000000</color> </resources>
myselector.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="@drawable/back"/> <item android:state_pressed="true" android:drawable="@drawable/backdown"/> </selector>
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent"> <ImageButton android:id="@+id/imageButton1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@color/back" android:src="@drawable/myselector"></ImageButton> </LinearLayout>
3. 状态开关
1) ToggleButton类
ToggleButton的状态只能是选中和未选中,并且需要为不同的状态设置不同的文本。
android:Off 未被选中时显示的文本
android:On 选中时显示的文本
实例3:ToggleButton的用法:
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, AndroidTest!</string> <string name="app_name">AndroidTest</string> <string name="off">关灯</string> <string name="on">开灯</string> </resources>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/bulb_off" android:id="@+id/ImageView01" android:layout_gravity="center_horizontal"></ImageView> <ToggleButton android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="140dip" android:textOff="@string/on" android:textOn="@string/off" android:id="@+id/Tb"></ToggleButton> </LinearLayout>
MyAnroidTest.java:
package qijia.si;
import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.ToggleButton; public class AndroidTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ToggleButton tb =(ToggleButton) findViewById(R.id.Tb); tb.setOnCheckedChangeListener(new OnCheckedChangeListener(){ //添加监听器 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ setBulbState(isChecked); } }); } public void setBulbState(boolean state){ ImageView iv = (ImageView) findViewById(R.id.ImageView01); iv.setImageResource((state)?R.drawable.bulb_on:R.drawable.bulb_off); //设置图片资源 ToggleButton tb = (ToggleButton) this.findViewById(R.id.Tb); tb.setChecked(state); //设置为选中状态 } }
发表评论
-
Android开发06—菜单与对话框(下)
2011-05-16 19:11 1115Android开发06—菜单与对 ... -
Android开发06—菜单与对话框(上)
2011-05-13 20:07 1188Android开发06—菜单与对话框(上) 1. 菜单 1) ... -
Android开发05—Android常用高级控件(下)
2011-05-09 19:16 1211Android开发05—Android常用高级控件(下) 1 ... -
Android开发04—Android常用高级控件(上)
2011-05-04 17:38 1846Android开发04—Android常用高级控件(上) 1 ... -
Android的adapter总结和深入研究
2011-05-04 17:35 1822Android的adapter总结和深 ... -
Android开发03—Android常用基本控件(下)
2011-05-02 17:14 945Android开发03—Android常用 ... -
Android系统开发02—Android布局管理器
2011-04-26 19:36 984Android系统开发02—Android布局管理器 1. ... -
Android系统开发01—Android基本组件
2011-04-26 19:33 934Android系统开发01—Android基本组件 1. 应 ...
相关推荐
android 常用控件如:ProgressBar、ActionBar、Switch、ListView 、PopupWindow....等的用法,各种Style的写法以及自定义常用控件 android 五大布局的汇总及用法
教程名称: 老罗Android开发视频教程-Android常用UI控件编程【32集】【】Android常用UI控件编程第七集【】Android常用UI控件编程第二十三集【】Android常用UI控件编程第二十九集【】Android常用UI控件编程第二十二...
NULL 博文链接:https://zzqrj.iteye.com/blog/1124365
主要讲解Android发展史、Android五大布局、Android基础常用控件、Android开发工具等讲解
Android移动平台开发-实验报告
包括了所OPHONE&android所有常用控件,手机开发必备 OPHONE &android常用控件 OPHONE &android常用控件
Android 中常用的控件介绍及其相应的使用,内含实例代码
Android UI控件组件库集合【源码】 热门标签,流式布局,刮刮乐控件,侧滑控件,循环幻灯片控件,自动换行控件,下拉列表,圆角进度控件等常用UI控件组件合集
雨松MOMO带大家盘点Android 开发中的常用系统控件 欢迎大家下载阅读。。
4.2 常用控件应用 4.2.1 文本框(Textiew) 4.2.2 列表(ListView) 4.2.3 提示(T0ast) 4.2.4 编辑框(EditText) 4.2.5 单项选择(RadioGroup、RadioButton 4.2.6 多项选择(CheckBox) 4.2.7 下拉列表(Spinner) 4.2.8 自动...
Android常用控件开发详解.pdf
【第一版第八章】老罗Android开发视频-常用UI控件(33集) 【第一版第九章】老罗Android开发视频--存储数据和文件(7集) 【第一版第十章】老罗Android开发视频--对话框介绍(4集) 【第一版第十一章】老罗...
这里面有很多android开发中所使用的控件,并且以一种交互式的界面将其展现出来,希望能对android开发初学者有所帮助。
1.5 更上一层楼——加入Android开发社区 1.6 本章小结 第2章 工欲善其事 必先利其器——搭建Android开发环境 2.1 开发Android应用前的准备 2.1.1 Android开发系统要求 2.1.2 Android软件开发包 2.1.3 其他注意事项 ...
Android开发常用整理(不断扩充中)包含各种工具类、线程池、日志、自定义的控件、程序崩溃捕捉处理、默认的Application配置、常用的Adapter等 注意: > 如果需要使用FragmentTabAdapter,则需要android-support-v4....
现在我们公司得app都是差不多得title,所以抽时间自己撸了个常用得标题栏控件,直接在布局中使用。 不过本人还是推荐使用官方toolbar 等官方 新控件 ,效果太炫了。无奈设计要求这样,所以在这个公司干,为了开发...
《卓越工程师培养计划"十二五"规划教材:Android嵌入式应用开发》主要内容包括Android基础知识、Android应用程序开发的基本流程、Android应用程序开发的常用编程接口、常用控件及一些在应用程序中常用的模块和功能的...
RxLibrary工程:一些常用的工具类,以及常用的控件,主要用来提高开发效率
包括开发环境搭建、Android语言基础、常用布局及控件、四大组件、图形图像技术、多媒体应用、数据处理技术、触摸和手势识别、多线程、网络技术、定位、蓝牙以及VR和NDK开发等知识,《Android开发实战:从学习到产品...