




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
常用控件在 Android 中使用各种控件(View)l TextView - 文本显示控件l Button - 按钮控件l EditText-l ImageButton - 图片按钮控件l ImageView - 图片显示控件l CheckBox - 复选框控件l RadioButton - 单选框控件l AnalogClock - 钟表(带表盘的那种)控件l DigitalClock - 电子表控件l ToggleButton-开关按钮控件1、TextView类似ASP.NET中的Label控件,只读显示控件,可通过getText()获取其android:text属性、setText()设置其android:text属性。在res/layout/main.xml的LinearLayout节中添加如下代码来声明TextView。TextView android:layout_ width=fill_parent android:layout_border=1 height=wrap_content android:text=string/hello android:id=+id/myTextView /在java代码中可以通过下列代码取得该控件。1 /取得该控件2 TextView myTextView =(TextView)findViewById(R.id.myTextView);TextView 的 Demotextview.xml代码_TextView.Java代码package com.webabcd.view;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class _TextView extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.textview);/ 设置 Activity 的标题setTitle(TextView);TextView txt = (TextView) this.findViewById(R.id.textView);/ 设置文本显示控件的文本内容,需要换行的话就用“ ”txt.setText(我是 TextView 显示文字用的);2、Button按钮控件,用户通过该控件来提交自己的请求,是用户与应用程序交互的最常用控件之一。1)普通按钮在res/layout/main.xml中声明控件 Button android:layout_ width=wrap_content android:layout_border=1 height=wrap_content android:id=+id/myButton /2)用户自定义按钮用户自定义按钮在很大程度上满足了用户个性化的要求。在这儿创建一个用图片代替普通文字的按钮。首先把图片复制进res/drawable*文件夹(三个文件夹都需要);然后再同一个文件夹中创建一个my_button.xml用户定义我们的格式选择文件; xml version=1.0 encoding=? selector xmlns:android=/apk/res/android item android:drawable=drawable/hmenulock android:state_pressed=true / item android:drawable=drawable/hmenuunlock android:state_focused=true / item android:drawable=drawable/hmenuunlock / selector当my_button.xml文件被引用后,上面的item的顺序就非常的重要了。因为在进行匹配时,android:state_pressed和switch的case一样,只要有一个item匹配成功,后面的就将不再进行下去。最后在res/layout/main.xml中引用我们刚才定义的my_button.xml。1 Button android:layout_ width=wrap_content android:layout_border=1 height=wrap_content android:background=drawable/my_button android:id=+id/myButton /android:background=”drawable/my_button”定义了引用的背景选择文件来自drawable文件夹的my_button.xml。上面简单的介绍了如何定义一个普通Button和自定义格式的Button。那么如何响应用户的动作呢? /响应Button的Click事件 myButton = (Button) findViewById(R.id.myButton); myButton.setOnClickListener(new OnClickListener() Override public void onClick(Viewv) / TODO Auto-generated method stub Toast.makeText(Android_Control_DemoActivity.this, click Button, Toast.LENGTH_SHORT).show(); );Button 的 Demobutton.xml代码_Button.java代码package com.webabcd.view;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class _Button extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.button);setTitle(Button);Button btn = (Button) this.findViewById(R.id.button);btn.setText(click me);/ setOnClickListener() - 响应按钮的鼠标单击事件btn.setOnClickListener(new Button.OnClickListener()Overridepublic void onClick(View v) TextView txt = (TextView) _Button.this.findViewById(R.id.textView);txt.setText(按钮被单击了););3、EditTextAndroid系统提供给用户输入的文本框,如ASP.NET中TextBox。EditView类继承自TextView类,EditView与TextView最大的不同就是用户可以对EditView控件进行编辑,同时还可以为EditView控件设置监听器,用来判断用户的输入是否合法。EditText的 DemoeditText.xml代码EditText android:layout_ width=fill_parent android:layout_border=1 height=wrap_content android:id=+id/myEditText /让我们的EditText一个回车动作,请看下面的代码,代码里演示了如何取得EditText的值以及如何为TextView赋值。 /操作EditText控件,取值以及响应事件 myEditText = (EditText)findViewById(R.id.myEditText); myEditText.setOnKeyListener(new OnKeyListener() Override public boolean onKey(View v, int keyCode, KeyEvent event) / 响应用户的回车键动作,将EditText中值显示到TextView中 if (event.getAction() = KeyEvent.ACTION_DOWN) &(keyCode = KeyEvent.KEYCODE_ENTER) myTextView.setText(myEditText.getText(); return true; return false; );4、ImageButton除了可以使用Android系统自带的Button(按钮)外,在Android平台中,我们还可以制作带图标的按钮,这就需要使用ImageButton组件鸟要制作带图标的按钮,首先要在布局文件中定义ImageButton,然后通过setImageDrawable方法来设置按钮要显示的图标。同样需要对按钮设置事件监听 setOnClickListener,以此来捕捉事件并处理。ImageButton 的 Demoimagebutton.xml代码 _ImageButton.java代码package com.webabcd.view;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageButton;import android.widget.TextView;public class _ImageButton extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.imagebutton);setTitle(ImageButton);ImageButton imgButton = (ImageButton) this.findViewById(R.id.imageButton);/ 设置图片按钮的背景imgButton.setBackgroundResource(R.drawable.icon01);/ setOnClickListener() - 响应图片按钮的鼠标单击事件imgButton.setOnClickListener(new Button.OnClickListener()Overridepublic void onClick(View v) TextView txt = (TextView) _ImageButton.this.findViewById(R.id.textView);txt.setText(图片按钮被单击了););5、ImageView ImageView用来显示任意图像图片,可以自己定义显示尺寸,显示颜色等等.android:adjustViewBounds 是否保持宽高比。需要与maxWidth、MaxHeight一起使用,单独使用没有效果。android:cropToPadding 是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用android:maxHeight 定义View的最大高度,需要与AdjustViewBounds一起使用,单独使用没有效果。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置:1) 设置AdjustViewBounds为true;2) 设置maxWidth、MaxHeight;3) 设置设置layout_width和layout_height为wrap_content。android:maxWidth 设置View的最大宽度。android:scaleType 设置图片的填充方式。android:src 设置View的图片或颜色android:tint 将图片渲染成指定的颜色。使用Martix(android.graphics.Matrix)类中的postScale()方法结合Bitmap来实现缩放图片的功能Bitmap bmp = BitmapFactory.decodeResource(getResource(),R.drawalbe.icon1)int bmpwidth = bmp.getWidth();int bmpheight = bmp.getHeight();Matrix matrix = new Matrix();matrix.postScale(width,height);Bitmap bm = Bitmap.createBitmap(bmp,0,0,bmpwidth,bmpheight ,matrix,true);imageView.setImageBitmap(bm);在Android中不允许ImageView在产生后,动态修改其长度和宽度,所以要实现图片发大缩小的功能,必须将原来的ImageView移除,重新产生一个新的ImageView,并且指定图片来源给它,再放入Layout中。ImageView 的 Demoimageview.xml代码_ImageView.java代码package com.webabcd.view;import android.app.Activity;import android.os.Bundle;import android.widget.ImageView;public class _ImageView extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.imageview);setTitle(ImageView);ImageView imgView = (ImageView) this.findViewById(R.id.imageView);/ 指定需要显示的图片imgView.setBackgroundResource(R.drawable.icon01);6、CheckBox复选按钮。在应用程序中如果让用户选择自己的喜好时,因为用户的喜好不可能只有一个,所以这时你必须使用复选按钮,允许用户多选。在res/layout/main.xml中声明控件。1 CheckBox android:text=CheckBox android:id=+id/checkBox1 android:layout_ width=wrap_content android:layout_border=1 height=wrap_contentCheckBox下面来响应用户的选择操作,通常情况下,复选框选择后是有一个按钮来统一提交的,但是我们在例子中只演示如何响应CheckBox的Change事件。 /为myChecBox控件绑定响应事件 myCheckBox = (CheckBox)findViewById(R.id.checkBox1); myCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) / TODO Auto-generated method stub Toast.makeText(Android_Control_DemoActivity.this, myCheckBox.getText(), Toast.LENGTH_SHORT).show(); );CheckBox 的 Democheckbox.xml代码 _CheckBox.java代码package com.webabcd.view;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.TextView;public class _CheckBox extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.checkbox);setTitle(CheckBox);CheckBox chk = (CheckBox) this.findViewById(R.id.chk1);/ setOnCheckedChangeListener() - 响应复选框的选中状态改变事件chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) TextView txt = (TextView) _CheckBox.this.findViewById(R.id.textView);txt.setText(CheckBox01 的选中状态: + String.valueOf(isChecked););7、RadioButton单选按钮,放在一个RadioGroup中,在这个group中只能有一个选项能够被选中,比如你选择性别时,只能选择一个性别。为了响应RadioButton的选择动作,我们可以有两种方式。A、 定义单独的一个 myClickListener监听器,然后为每一个Radio注册这个监听器。 /1、定义一个我们自己的 myClickListener监听器,事件类型为OnClickListener OnClickListener myClickListener=new OnClickListener() Override public void onClick(View v) / TODO Auto-generated method stub RadioButton r=(RadioButton)v; Toast.makeText(Android_Control_DemoActivity.this, r.getText(), Toast.LENGTH_SHORT).show(); ; final RadioButton radio_1=(RadioButton)findViewById(R.id.radio0); final RadioButton radio_2=(RadioButton)findViewById(R.id.radio1); final RadioButton radio_3=(RadioButton)findViewById(R.id.radio2); radio_1.setOnClickListener(myClickListener); radio_2.setOnClickListener(myClickListener); radio_3.setOnClickListener(myClickListener);复制代码B、 为RadioGroup绑定事件,在这个事件中判断选择的按钮ID来响应事件。 /2、使用RadioGroup的CheckedChange方法来监听我们的RadioButton的点击事件 myRadioGroup =(RadioGroup)findViewById(R.id.radioGroup1); myRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() Override public void onCheckedChanged(RadioGroup group, int checkedId) / TODO Auto-generated method stub Toast.makeText(Android_Control_DemoActivity.this, radio_1.getText(), Toast.LENGTH_SHORT).show(); if (radio_2.getId()=checkedId) Toast.makeText(Android_Control_DemoActivity.this, radio_2.getText(), Toast.LENGTH_SHORT).show(); Toast.makeText(Android_Control_DemoActivity.this, radio_3.getText(), Toast.LENGTH_SHORT).show(); );以上两种方法有什么区呢?A方法是只要你点击单选按钮系统就会响应,而不管你点击的是否为同一个RadioButton,B方法只会响应不同的单选动作,如果在同一个RadioButton上重复点击,系统是不会响应的。RadioButton 的 Demoradiobutton.xml代码_RadioButton.java代码package com.webabcd.view;import android.app.Activity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;public class _RadioButton extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.radiobutton);setTitle(RadioButton);RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup);/ setOnCheckedChangeListener() - 响应单选框组内的选中项发生变化时的事件group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) TextView txt = (TextView) _RadioButton.this.findViewById(R.id.textView);txt.setText(RadioButton)findViewById(checkedId).getText() + 被选中););8、AnalogClock这是一个带有时针和分针的模拟时钟控件。AnalogClock 的 Demo analogclock.xml代码_AnalogClock.java代码package com.webabcd.view;import android.app.Activity;import android.os.Bundle;public class _AnalogClock extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) / TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.analogclcok);setTitle(AnalogClock);9、DigitalClockDigitalClock只需要在布局文件中,用于显示时间的地方使用此组件即可,无需用java代码进行实例化。DigitalClock并没有设置输出格式的属性或方法,但是可以通过继承重写来实现,Digit
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 韵母的课件(幼儿园)
- 2025年拉杆球头项目建议书
- 香坊区中考二模语文试题(图片版含答案)
- 系统解剖学试题及答案(九)
- 2025年混合式步进电机项目发展计划
- 2025年转向齿条项目合作计划书
- 五年级语文教案 (一)
- 2025年AOI光学检测系统合作协议书
- 2025年电子测量仪器合作协议书
- 2025年互联网+政务服务在推动政府职能转变中的关键作用
- 公司DFMEA样表模板
- 2023-2024学年贵州省贵阳市小学语文三年级下册期末模考考试题
- RB/T 069-2021居家养老服务认证要求膳食服务
- JJG 648-2017非连续累计自动衡器(累计料斗秤)
- 品牌战略定位课件
- 2022年武汉东湖学院辅导员招聘考试笔试试题及答案解析
- 医疗技术分级授权与再授权申请表
- 儿童腺病毒肺炎诊疗规范课件
- MBTI人格理论教学课件
- DB65∕T 2810-2009 核桃玛仁糖-行业标准
- 商业银行风险预警系统整体架构设计
评论
0/150
提交评论