构建用户界面和使用控件.DOC

上传人:天*** 文档编号:338706 上传时间:2018-09-23 格式:DOC 页数:56 大小:1.19MB
下载 相关 举报
构建用户界面和使用控件.DOC_第1页
第1页 / 共56页
构建用户界面和使用控件.DOC_第2页
第2页 / 共56页
构建用户界面和使用控件.DOC_第3页
第3页 / 共56页
构建用户界面和使用控件.DOC_第4页
第4页 / 共56页
构建用户界面和使用控件.DOC_第5页
第5页 / 共56页
点击查看更多>>
资源描述

1、 构建用户界面和使用控件 Android 中的常见控件: 一、文本控件 1 TextView TextView 类的定义如下,它属于 View 类的直接子类,主要功能是用于显示文本。 java.lang.Object android.view.View android.widget.TextView 主要属性 所有组件都会在 R.java 中自动生成 public static final class id public static final int textView01=0x7f050000; 在程序代码中,可以通过 setAutoLinkMask 方法进行 autoLink 行为的 设

2、置 , 传递一个 int 类型的值,如 Linkify.EMAIL_ADDRESS 或 Linkify.WEB_ADDRESS 或者使用 Linkify 类的静态方法 addLinks Linkify.addLinks(tv,Linkify.ALL); 设置背景颜色 有三种方法: setBackgroundResource:通过颜色资源 ID设置背景色。 setBackgroundColor:通过颜色值设置背景色。 setBackgroundDrawable:通过 Drawable 对象设置背景色。 setBackgroundResource 方法设置背景: 以下为引用内容: textView

3、.setBackgroundResource(R.color.background); setBackgroundColor 方法设置背景: 以下为引用内容: textView.setBackgroundColor(android.graphics.Color.RED); setBackgroundDrawable 方法设置背景: 以下为引用内容: Resources resources=getBaseContext().getResources(); Drawable drawable=resources.getDrawable(R.color.background); textView.s

4、etBackgroundDrawable(drawable); 等价于 textView.setBackground(getResources().getDrawable(R.color.background) 设置背景图片 1、将背景图片放置在 drawable-mdpi 目录下,假设图片名为 jobs.jpg 。 2、 main.xml 文件 以下为 引用内容: 为 TextView 编写 style 在 Android 中为了方便美工对组件进行修饰,也可以使用一些样式文件对组件显示进行控制,用户只需要按照如下的 xml 文件格式即可定义组件的显示格式, 方法: res 的 values

5、目录,复制 strings.xml 文档进行修改即可 30px #0000FF wrap_content wrap_content all 如果要引用样式表文件, 2 EditText 样式表 20px #0000FF wrap_content wrap_content all 20px #0000FF wrap_content wrap_content fill_parent wrap_content 10pt drawable/bg_border android:hint可以提示文本,此文本将以较暗一些的颜色显示,并在用户开始键入内容时立即消失。提示的用途是让用户知道应在此字段中输入何种内

6、容,无需用户选择或擦除文本。 在代码中,可以通过调用 setHint方法进行设置。 二、按钮控件 1. Button 处理单击事件: 方法一:略 方法二: 为 Button设置 androi:onClick属性,并在 Activity文件中编写相应的方法,该方法必须有一个 View类型的参数 main.xml ButtonClickActivity.java public void clickOne(View v) (TextView)v).setText(R.string.newtext); tv.setText(R.string.newtext); 方法 3: Activity本身作为事件

7、监听器 public class NowActivity extends Activity implements OnClickListener /* Called when the activity is first created. */ Button btn; TextView tv; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); btn=(Button)findViewById(R.id

8、.time); tv=(TextView)findViewById(R.id.text); btn.setOnClickListener(this); public void onClick(View view) updateTime(); public void updateTime() tv.setText(new Date().toString(); 如果有多个按钮,判断是哪个按钮被按下 class MyButtonOnClick implements OnClickListener Override public void onClick(View v) / TODO Auto-gen

9、erated method stub if(v=myButton) tv.setText(R.string.click1); else tv.setText(R.string.click2); 2. ImageButton ImageButton btn =(ImageButton)this.findViewById(R.id.imageBtn); btn.setImageResource(R.drawable.icon); 按钮的图像文件必须位于 res/drawable文件夹下 选择器 将该文件放在 res/drawable文件夹下,将按钮的 android:src属性设置为选择器 xml

10、文件 android:src= “drawable /imagebuttonselector“ 3. ToggleButton ToggleButton是一种具有两种状态的按钮,此按钮可以处于 On状态,也可以处于 Off状态,默认行为时在 On状态下显示一个绿条,并将文本设置为 On,在 Off状态下显示一个灰条。并将文本设置为 Off.也可以通过 textOn和 textOff属性修改 ToggleButton的文本。 4. CheckBox java.lang.Object android.view.View android.widget.TextView android.widget.

11、Button android.widget.CompoundButton android.widget.CheckBox CheckBox 类本身没有找到合适的方法,此时可以到它的父类中去找,如 CompoundButton父类中去找 常用方法: 1. public boolean isChecked() 2. public void setChecked(boolean checked) 3.public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) 4. public voi

12、d toggle() /Change the checked state of the view to the inverse of its current state ch1.setOnCheckedChangeListener(new OnCheckedChangeListener() Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) / TODO Auto-generated method stub if(arg1) System.out.println(“swim“); ); 5.Radio

13、Button RadioGroup 与 RadioButton 单选钮 java.lang.Object android.view.View android.view.ViewGroup android.widget.LinearLayout android.widget.RadioGroup 在许多语言中,单选钮直接定义即可,但在 Android中, RadioGroup定义的只是一个单选钮的容器,在这个容器中要加入多个单选项,而这个单选项就是 RadioButton java.lang.Object android.view.View android.widget.TextView and

14、roid.widget.Button android.widget.CompoundButton android.widget.RadioButton 常用方法: 1. public void check(int id) /设置要选中的按钮编号 2. public void clearCheck() /清空选中状态 3. public int getCheckedRadioButtonId() /取得选中的 RadioButton的 ID 4. public void setOnCheckedChangedListener (RadioGroup.OnCheckedChangedListene

15、r listener) sexGroup.setOnCheckedChangeListener( new RadioGroup .OnCheckedChangeListener() Override public void onCheckedChanged(RadioGroup arg0, int arg1) / TODO Auto-generated method stub ); 判断是哪一个按钮被 checked public void onCheckedChanged(RadioGroup arg0, int arg1) / TODO Auto-generated method stub if(maleButton.getId()=arg1) System.out.println(“male“); else if(femaleButton.getId()=arg1) System.out.println(“female“); 或者采用以下方式:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 重点行业资料库 > 1

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。