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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-18 我要评论
想了解Android中的Button自定义点击效果实例代码的相关内容吗,在本文为您仔细讲解Button自定义点击效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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"/>

 方法二
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;
   }
  });

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们