Android_Button(ImageButton,RadioButton,ToggleButton,Switch)详解
来源:互联网 发布:全职高手网络剧演员表 编辑:程序博客网 时间:2024/05/15 07:08
1.概述
Button按钮组成可以为文本或者图标或者两者的集合,并为按钮设置相应的动作。
(1).With text, using the Button class<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_text"... />(2).With an icon, using the ImageButton class
<ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/button_icon"... />(3).With text and an icon, using the Button class with the android:drawableLeft attribute,可在text的四周合适的位置添加图片
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_text"android:drawableLeft="@drawable/button_icon"... />
2.响应事件
(1).Add the android:onClick attribute to the <Button> element in XML layout.
<?xml version="1.0" encoding="utf-8"?><Button xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/button_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_send"android:onClick="sendMessage" />/** Called when the user touches the button */public void sendMessage(View view) { // Do something in response to button click}(2).Using an OnClickListener
Declare the click event handler pragmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.
Button button = (Button) findViewById(R.id.button_send);button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do something in response to button click }});
3.自定义背景
(1).将多个图片置于 res/drawable/ 目录下
(2).创建一个xml文件(custom.xml),置于
res/drawable/目录下
<?xml version="1.0" encoding="utf-8"?><!-- custom.xml --><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:drawable/pic1" android:state_pressed="true" /> <item android:drawable="@android:drawable/pic2" /> <!-- <item>标签元素的位置很重要,当这个xml文件被引用时,会从前往后找到一个适合的item标签,默认的至于最后 --></selector>(3).标签使用background属性
<Buttonandroid:id="@+id/button_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_send"android:onClick="sendMessage"android:background="@drawable/custom" />default
press
4.Radio Buttons
To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside aRadioGroup. By grouping them together, the system ensures that only one radio button can be selected at a time.TheRadioGroup is a subclass of LinearLayoutthat has a verticalorientationby default.RadioButton是LinearLayout的子类
<?xml version="1.0" encoding="utf-8"?><RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><RadioButton android:id="@+id/radio_pirates"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/pirates"android:onClick="onRadioButtonClicked"/><RadioButton android:id="@+id/radio_ninjas"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/ninjas"android:onClick="onRadioButtonClicked"/></RadioGroup>public void onRadioButtonClicked(View view) {// Is the button now checked?boolean checked = ((RadioButton) view).isChecked(); // Check which radio button was clickedswitch(view.getId()) {case R.id.radio_pirates:if (checked)// Pirates are the bestbreak;case R.id.radio_ninjas:if (checked)// Ninjas rulebreak;}}viewPager与RadioButton的对应关系/**根据viewPager选中postion,指定哪个radioGroup被选中*/@Overridepublic void onPageSelected(int position) {group.check(group.getChildAt(position).getId());}/**根据radioGroup选中的position,指定viewPager的当前页*/@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {viewPager.setCurrentItem(group.indexOfChild(group.findViewById(checkedId)));}
5.Toggle Buttons / Switches
ToggleButton 和Switch控件是CompoundButton的子类 , so you can implement their behavior the same way.You can add a basic toggle button to your layout with the ToggleButton object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that provides a slider control, which you can add with a Switch object.
Toggle buttons
Switches (in Android 4.0+)
<ToggleButton android:id="@+id/togglebutton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textOn="Vibrate on"android:textOff="Vibrate off"android:onClick="onToggleClicked"/>ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);//设置监听器toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { // The toggle is enabled } else { // The toggle is disabled } }});//或者另一种方法设置public void onToggleClicked(View view) { // Is the toggle on? boolean on = ((ToggleButton) view).isChecked(); if (on) { // Enable vibrate } else { // Disable vibrate }}
- Android_Button(ImageButton,RadioButton,ToggleButton,Switch)详解
- 004 RadioButton,ToggleButton和Switch控件
- android *** Layout 06 RadioButton && ToggleButton && Switch
- CompoundButton之RadioButton、CheckBox、ToggleButton、Switch
- 【Android 开发教程】Button,ImageButton,EditText,ChcekBox,ToggleButton,RadioButton
- Button、ImageButton、EditText、CheckBox、ToggleButton、RadioButton和Radio
- 设置ToggleButton、Switch、CheckBox和RadioButton的显示效果
- 设置ToggleButton、Switch、CheckBox和RadioButton的显示效果
- Android UI之选择器-ToggleButton、Switch、CheckBox和RadioButton
- 设置ToggleButton、Switch、CheckBox和RadioButton的区别
- Android之RadioButton与RadioGroup,CheckBox,Switch,ToggleButton的介绍
- android整合--UI基本控件Button,ImageButton,EditText,ChcekBox,ToggleButton,RadioButton
- Android学习笔记---第四天---基础UI组件---RadioButton&CheckBox&ToggleButton&Switch---Button的子类
- android开关按钮,Switch,ToggleButton,RadioButton,CheckBox ,多种实现开关按钮
- Android_Button
- ToggleButton和RadioButton
- Android ToggleButton和Switch
- ToggleButton switch开关
- 杭电2020 绝对值排序
- OpenGL MFC Control
- ubuntu12.04编译android源码环境搭建____安装相关包
- iOS应用开发视频教程笔记(十三)Core Data
- iOS: UIScrollViewDelegate Protocol的方法scrollViewDidEndDecelerating:
- Android_Button(ImageButton,RadioButton,ToggleButton,Switch)详解
- 内核编译及模块相关命令使用
- openstack nova目录解析
- 南阳理工ACM 540 奇怪的排序
- iOS: UIScrollViewDelegate Protocol的方法scrollViewDidEndScrollingAnimation:
- 我会努力提升自己
- 在eclipse下远程调试hadoop2.0
- Ones
- C语言二叉树的基本操作