Android中的Button自定义点击效果
方法一
1.放在drawable下的selector.xml文件
<?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/temp1" />
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@drawable/temp2" />
<item android:state_focused="true" android:drawable="@drawable/temp3" />
<item android:state_focused="false" android:drawable="@drawable/temp4" />
</selector>
2.布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:drawableTop="@drawable/shouru"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:background="@drawable/selector"/>
</LinearLayout>
3.只是为了测试,所以效果不是很好,Button部分状态效果图如下:
①初始化的时候默认显示的按钮效果:
②点击后释放显示的效果
③点击不放时的效果
方法二
1.布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button"
android:drawableTop="@drawable/shouru"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:background="@drawable/temp4"/>
</LinearLayout>
2.主要的java代码,实现点击效果:
Button button = (Button) this.findViewById(R.id.button);
button.setOnTouchListener(new Button.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
v.setBackgroundResource(R.drawable.temp1);
Log.i("TestAndroid Button", "MotionEvent.ACTION_DOWN");
}
else if(event.getAction() == MotionEvent.ACTION_UP){
v.setBackgroundResource(R.drawable.temp2);
Log.i("TestAndroid Button", "MotionEvent.ACTION_UP");
}
return false;
}
});
原文地址:http://www.cnblogs.com/jqyp/archive/2010/12/10/1902498.html
(转发请注明转自:学PHP)
- Android 以特定格式显示当前时间 (2012-09-26 22:38:08)
- Android位置策略(一) (2012-09-29 07:19:04)
- Android Context 详解 (2012-09-29 07:20:30)
- Android 广播大全 Intent Action 事件 (2012-09-29 07:20:38)
- [Android_机制]_Http和Socket连接区别.doc (2012-11-24 00:28:39)
- onCreateOptionsMenu中 图标,文字创建 (2012-09-25 21:54:17)
- android 学习笔记十一 可视化频率(频谱) (2012-09-10 23:13:19)
- Android之进程与线程 (2012-09-10 23:13:07)
- [android]控件ImageView的常用属性 (2012-09-04 15:12:07)
- android 获取应用证书及签名信息 (2012-09-04 15:12:01)