常用控件 01 按钮 Button
来源:互联网 发布:adobe创意云 mac 编辑:程序博客网 时间:2024/05/20 17:28
1.创建3种类型的 Button :
1>只有文字的,用 Button 类,如:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_text" ... />2>带有图片的,用 ImageButton 类,如:
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/button_icon" ... />3>有图片又有文字的,用 Button 类用 android:drawableLeft 属性:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_text" android:drawableLeft="@drawable/button_icon" ... />
2.处理单击事件:
方法1>在xml文件中添加 android:onClick 属性,其值为处理该事件的方法名。然后在对应的Activity中实现该方法。如:
在xml中添加 Button,
<?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" />
然后在对应的 Activity 中实现:
/** Called when the user touches the button */public void sendMessage(View view) { // Do something in response to button click}方法2>使用 OnClickListener :
当运行过程中实例化 Button或者需要在 Fragment 子类中声明点击行为的时候,需要用到 OnClickListener。
要使用OnClickListener,先创建一个View.OnClickListener类,然后调用setOnClickListener方法将其分配给 button。如:
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.Button样式:
1>可以在<application>中声明主题,如:android:theme="@android:style/Theme.Holo"
2>用 android:background 来定制Button的背景。
3>没有边框的Button:
style="?android:attr/borderlessButtonStyle"
4>自定义背景:在xml文件中为图片背景创建一个包含3种状态的状态列表:
①制作分别代表默认,按下,获得焦点3种状态的位图。为保证适合不同尺寸,最好做成Nine-patch图片
②将位图放置在当前项目的 res/drawable/ 目录下
③在 res/drawable/ 目录下新建一个xml文件,如 button_custom.xml:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_pressed" android:state_pressed="true" /> <item android:drawable="@drawable/button_focused" android:state_focused="true" /> <item android:drawable="@drawable/button_default" /></selector>item的顺序有关,例如上方的代码中,只有当press和focuse都为false时才轮到default④将该xml文件添加到该 Button 的 android:background 属性中:
android:background="@drawable/button_custom"
- 常用控件 01 按钮 Button
- 按钮控件(button)
- 按钮控件(button)
- android 控件 按钮(Button)
- 几个常用的控件TextView EditText Button MENU按钮
- LibGDX_6.3: 常用系统控件: 按钮(Button)
- 按钮控件(Button,LinkButton,ImageButton)
- Android控件复习:按钮(Button)
- Android基本控件:button按钮
- MFC按钮控件(Button)
- Android 控件之一:Button 按钮
- Qt按钮控件(Button)
- MFC透明BUTTON(按钮)控件
- 2.常用控件:Button
- VS2010/MFC编程入门之二十一(常用控件:按钮控件Button、Radio Button和Check Box)
- VS2010/MFC编程入门之二十一(常用控件:按钮控件Button、Radio Button和Check Box)
- VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
- VS2013/MFC编程入门之二十一(常用控件:按钮控件Button、Radio Button和Check Box)
- hibernate constrained属性
- __try,__except,__finally,__leave
- Linux AutoMake
- Freescale 9S12 系列单片机应用笔记(SCI)3
- linux kobject与sysfs自学报告
- 常用控件 01 按钮 Button
- 数据库笔记二
- pin 的使用简介 ——环境设置 基本工具的使用 相关知识
- JAVA的MD5加密与解密
- J2ME开发中实现文字的自动换行
- IIS中另一个程序正在使用此文件,进程无法访问
- 关于Whisperwind数学库的选择:XnaMath
- Hadoop全分布安装配置及常见问题
- 组态