方法一

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;
}
});

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. android 使用私有maven仓库发布、打包、引用
  4. 关于Android(安卓)混淆的内容总结
  5. android 9.0 SD卡权限问题 文件管理器没有权限
  6. Android(安卓)Button 点击时替换背景颜色和替换当前图标
  7. Android(安卓)DEX安全攻防战
  8. Android代码混淆官方实现方法
  9. Android(安卓)Dex文件结构

随机推荐

  1. 配置android模拟器加速(虚拟化加速)
  2. 10.Binder进阶:系统服务中的Binder
  3. Android(安卓)Jetpack 之 App Startup
  4. Android下Affinities和Task!!!!!!!!!!!!
  5. Android(安卓)小功能之 TextView 只设置
  6. Android(安卓)MVP架构
  7. 面试 Google, 我失败了 | Google 面经分
  8. 面试必备:Android(安卓)Activity启动流程
  9. Android:Material Design之NavigationView
  10. Android(安卓)LaunchMode SingleInstance