1. Android 中的UI开发
- 常见控件:文本字段、按钮、列表、网格 [Android SDK提供]
- 控件核心:android.view.View类[常见控件]、android.view.ViewGroup类[布局列表基类]
- 用户界面构建:完全用代码实现 or XML中定义 or 两者结合[XML构建布局 代码填充动态数据]
2. Android 中的常见控件
# 文本控件 [TextView、EditText、AutoCompleteTextView、MultiCompleteTextView]
· TextView 控件
- XML简单规范 【指定ID 高度 宽度 文本值】
<TextView
android:id = "@+id/name"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/name_text"
/>
- TextView控件,知道如何显示文本,但不允许进行编辑
- 超链接属性 autoLink
~ 应用:显示URL或者E-mail地址时,将autoLink属性设置为“email|web”
该控件找到并突出显示这些电子邮件和URL,而且用户点击时系统启动对应应用程序或浏览器
~ XML内容
<TextView
...
android:autoLink = "email|web"
...
/>
~ autoLink值集:web、email、phone、map 或者all[以竖线分割]
~ Java代码:使用setAutoLinkMask()方法,[android.text.util.Linkify类]
传递int值表示各种值的集合[Linkify.EMAIL_ADDRESSES|Linkify.WEB_ADDRESSES]
/*在设置 文本之前设置自动连接选项
*在设置文本之后设置连接选项不会影响现有文本
*此时使用代码添加文本连接,所以XML中不需要添加相应特殊属性
*/
TextView tv = (TextView) this.findViewById(R.id.tv);
tv.setAutoLinkMask(Linkify.ALL);
tv.setText("http://www.androidbook.com 或者 [email protected]");
~ Java -- addLinks()
Linkify类静态方法addLinks()方法,根据需要查找的内容添加连接。此时无序使用setAutoLinkMask
Linkify.addLinks() //设置文本以后使用该代码。单击一个链接会为该操作调用默认的intent
· EditText 控件 [TextView 子类]
- 支持文本编辑
- input属性:"textAutoCorrect" 更正常见拼写错误
"textCapWords" 单词转换为大写
指定input属性时,忽视较老的属性
- 老式多行键入默认行为:input属性设置为 textMultiLine [不指定时默认为单行]
- 输入提示:指定文本,并以稍暗的颜色显示,且在键入文本时立即消失
~ XML:android:hint = "your hint text here"
或者 android:hint = "@string/your_hint_name"//此时可在/res/values/strings.xml找到资源
~ Java:使用CharSequence 或 资源ID调用setHint()方法
· AutoCompleteTextView 控件
- 自动完成功能 -- 完整文本
- 代码
<AutoCompleteTextView
android:id = "@+id/activ"
android:layout_width = "fil_parent"
android:layout_height = "wrap_content"
/>
//对于用户的输入 提供自动补全建议
//创建适配器保存建议
AutoCompleteTextView actv = (AutoCompleteTextView) this.findViewById(R.id.actv);
ArrayAdapter<String> aa = new ArrayAdapter<string>(this,
android.R.layout.simple_dropdown_item_1line,
new String[]{"English","Hebrew","Hindi","Spanish","Greek"});
actv.setAdapter(aa);
· MultiCompleteTextView 控件
- 自动完成功能 -- 句子中完成每个单词的建议
- 使用该控件时必须告诉控件要在何处再次开始建议
- 代码
<MultiAutoCompleteTextView
android:id = "@+id/mactiv"
android:layout_width = "fil_parent"
android:layout_height = "wrap_content"
/>
//将逗号键入到字段中时 触发建议
MultiAutoCompleteTextView mactv=(MultiAutoCompleteTextView)this.findViewById(R.id.mactv);
ArrayAdapter<String> aa2 = new ArrayAdapter<string>(this,
android.R.layout.simple_dropdown_item_1line,
new String[]{"English","Hebrew","Hindi","Spanish","Greek"});
mactv.setAdapter(aa2);
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer);
# 按钮控件
· 按钮控件
- 处理单击事件
- 基本按钮类 android.widget.Button
- 代码 -- 处理按钮上的单击事件
<Button
android:id = "@+id/button1"
android:text = "@string/basicBtnLabel"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
/>
/*通过调用包含OnClickListener 的 setOnClickListener()方法注册单击事件
*/
Button button1 = (Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.asd.com"));
startActivity(intent);
}
});
- 代码 -- 设置按钮的单击处理程序
<Button ... android:onClick = "myClickHander" ... />
public void myClickHander(View terget){
switch(target.getId()){
case R.id.button1:
...
}
...
}
· ImageButton 控件
- 类:android.widget.ImageButton
- 按钮图像文件位置: /res/drawable
- 代码
<ImageButton
android:id = "@+id/imageButton2"
android:width = "wrap_content"
android:height = "wrap_content"
android:onClick = "myClickHander"
android:src = "@drawable/icon"
/>
ImageButton imageButton2 = (ImageButton)this.findViewById(R.id.imageButton2);
//imageButton2.setImageResource(R.drawable.icon); 图标设置一次就可以
· ToggleButton 控件
- 具有两种状态的按钮 例如:On[绿条] Off[灰条]
- 文本修改:android:textOn、android:textOff [打开关闭文本为独立特性]
- 代码
<ToggleButton
android:id = "@+id/cctglBtn"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Toggle Button"
android:textOn = "Run"
android:textOff = "Stop"
/>
· CheckBox 控件
- 两种状态
- 调用setChecked()或者toggle()管理复选框状态
- 获取状态:调用 isChecked()
- 逻辑实现:调用setOnCheckedChangeListener() 并实现OnCheckedChangeListener接口
- 代码 -- 创建复选框
<Linearlayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<CheckBox
android:id = "@+id/chickenCB"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Chicken"
/>
<CheckBox
android:id = "@+id/fishCB"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Fish"
/>
<CheckBox
android:id = "@+id/steakCB"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Steak"
/>
</Linearlayout>
- Java代码 -- 使用复选框
public class CheckedBoxActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);
CheckBox fishCB = (CheckBox)findViewById(R.id.fishCB);
fishCB.setOnCheckedChangeLinstener(
new CompoundButton.OnCheckedChangeListener(){
@Override
public void OnCheckedChanged(CompoundButton arg0, boolean isChecked){
Log.v("......");
}
});
}
}
· RadioButton 控件
- 单选按钮
- 单选按钮通常属于一个组,每组一次只能让一个选项被选中
- 代码 XML
<Linearlayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<RadioGroup
android:id = "@+id/BtnGrp"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
>
<RadioButton
android:id = "@+id/fishRBtn"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Fish"
/>
<RadioButton
android:id = "@+id/chickenRBtn"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Chicken"
/>
<RadioButton
android:id = "@+id/steakRBtn"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Steak"
/>
</RadioGroup>
</Linearlayout>
# ImageView控件
· 作用:显示图像
· 图像来源:文件、ContentProvider、图形对象、指定颜色
· 指定方式
XML: android:src="..."
java: setImageResource()、setImageBitmap()、setImageDrawable()、setImageURI()
# 日期和时间控件
· 主要控件:DatePicker TimePicker DigitalClock AnalogClock控件
· DatePicker、TimePicker 控件
- DatePicker选择日期 TimePicker选择时间
- 默认值值设备所知的当前日期和时间
· DigitalClock、AnalogClock控件
- 模拟时钟
- 时钟控件,不支持时间修改,唯一功能显示当前时间
- 可自行更新,无需其它操作
# MapView控件
· com.google.android.maps.MapView控件,可以显示地图
· 使用其活动必须扩展MapActivity,处理加载地图的多线程请求等
。。。待更新