Android中的Button自定义点击效果实例代码

杨建峰核雕

杨建峰核雕

2016-02-19 09:35

有一种朋友不在生活里,却在生命力;有一种陪伴不在身边,却在心间。图老师即在大家的生活中又在身边。这么贴心的服务你感受到了吗?话不多说下面就和大家分享Android中的Button自定义点击效果实例代码吧。

方法一
1.放在drawable下的selector.xml文件
代码如下:

android="http://schemas.android.com/apk/res/Android"

   android:drawable="@drawable/temp2" /

2.布局文件main.xml
代码如下:

http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    /
 android:drawableTop="@drawable/shouru"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/button"
 android:background="@drawable/selector"/

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

 方法二
1.布局文件main.xml
[code]
http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    /
 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"/

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

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
展开更多 50%)
分享

猜你喜欢

Android中的Button自定义点击效果实例代码

编程语言 网络编程
Android中的Button自定义点击效果实例代码

Android自定义格式显示Button的布局思路

编程语言 网络编程
Android自定义格式显示Button的布局思路

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

android dialog自定义实例详解

编程语言 网络编程
android dialog自定义实例详解

Excel中自定义函数实例剖析

办公软件
Excel中自定义函数实例剖析

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

jsp 自定义标签实例

Web开发
jsp 自定义标签实例

Excel自定义筛选实例

电脑入门
Excel自定义筛选实例

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

别用幸福亏欠幸福 - QQ情侣分组

别用幸福亏欠幸福 - QQ情侣分组

有时候 真希望自己是透明的 - QQ非主流分组

有时候 真希望自己是透明的 - QQ非主流分组
下拉加载更多内容 ↓