Android控件复习:按钮(Button)
来源:互联网 发布:中国气象数据网账号 编辑:程序博客网 时间:2024/05/17 23:41
主要内容:
1.按钮监听和长按按钮监听
2.“.9图片文件的使用”
3.自定义点击按钮效果
-图片模式
-颜色模式
实代码例:
1.部分代码
先来看布局文件main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" android:textSize="25px"/><TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30px"/><Button android:layout_width="90dip" android:layout_height="45dip" android:layout_marginTop="1dip" android:textSize="30px" android:background="@drawable/image_btn_bird" android:layout_gravity="center"/><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" android:background="@drawable/button_font"/><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/send_button_off"/><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/new1"/><Button android:id="@+id/btTest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test" android:onClick="selfDestruct"/></LinearLayout>监听code
//普通监听 button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "点击了button", Toast.LENGTH_LONG).show();}}); //长监听 button1.setOnLongClickListener(new OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "长按button", Toast.LENGTH_LONG).show();return true;}});
2. .9格式图片在开发中应用的非常多,我们不能保证每部手机都能显示标准,.9文件的图片可以根据屏幕来自动的伸缩,保证界面友好
可以看出在这个按钮中,我直接使用了new1.9.png的图片格式,黑线部分就是自己处理的,处理方法
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/new1"/>但是这个点击是没有效果的,只是孤零零的一张图片。我们下面加上判断来切换不同的图片显示按钮效果;
3.自定义效果
图片模式:code 使用true和false来判断
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/send_button_on" /> <item android:state_pressed="false" android:drawable="@drawable/send_button_off" /> </selector>颜色模式:code 和图片时一类的
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"> <shape> <solid android:color="#ff9d77"/> <stroke android:width="2dp" android:color="#dcdcdc" /> <padding android:left="30dp" android:top="10dp" android:right="30dp" android:bottom="10dp" /> </shape> </item> <item android:state_pressed="true"> <shape> <solid android:color="#ff00ff"/> <stroke android:width="2dp" android:color="#ff00ff" /> <corners android:radius="2dp" /> </shape> </item> </selector>
我们可以在应用中反复调用我们的文件
源代码
- Android控件复习:按钮(Button)
- Android复习之按钮button
- android 控件 按钮(Button)
- Android基本控件:button按钮
- Android 控件之一:Button 按钮
- Android Button按钮控件美化方法
- android 学习 ---控件按钮,Button 与ImageButton,
- 按钮控件(button)
- 按钮控件(button)
- Android中的基本控件(上)--按钮控件Button
- Android 按钮类控件大集锦:Button ToggleButton CheckBox RadioButton
- Android学习2控件之Button按钮组件
- 按钮控件(Button,LinkButton,ImageButton)
- 常用控件 01 按钮 Button
- MFC按钮控件(Button)
- Qt按钮控件(Button)
- MFC透明BUTTON(按钮)控件
- Android自定义Button按钮
- 音视频即时通讯开发平台的开发现状
- POJ 3207 Ikki's Story IV - Panda's Trick(2-SAT判定)
- 使用ES文件浏览器通过Android设备访问Win7电脑上的共享文件
- 内存泄漏检测工具
- Java 输入输出流教程(三)——【管道】(原作:Java IO Tutorial by Jakob Jenkov)
- Android控件复习:按钮(Button)
- OpenCV与Compressive Tracking实现人脸的实时检测与跟踪
- 深入浅出 - Android系统移植与平台开发(八)- HAL Stub框架分析
- 建筑的永恒之道----道之核心
- 指针与二维数组
- vs2010旗舰版 安装失败原因
- 建筑的永恒之道----译后记
- struts2 第一个hello word 程序
- filter会执行两次?Why?