zoukankan      html  css  js  c++  java
  • Android中的Button自定义点击效果

    原来听很多人说Button不能使用自定义的点击或选中效果,所以做下测试

    结果显示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;
       }
      });

    这个方法实现的效果和上面的一样,大家可以视具体情况来选择

  • 相关阅读:
    记Java程序Log4J漏洞的解决过程
    IIS中应用程序池自动停止,重启报错
    如何查看域名所对应的证书有效期?
    查看前端Vue版本命令
    WCF中常见的报错:The content type text
    dotnet 将自动代码格式化机器人带入团队 GitLab 平台
    WPF 引用第三方库的控件在设计器加上设计时数据和属性
    dotnet OpenXML 聊聊文本段落对齐方式
    WPF 布局 在有限空间内让两个元素尽可能撑开的例子
    dotnet 通过 DockerfileContext 解决项目放在里层文件夹导致 VisualStudio 构建失败
  • 原文地址:https://www.cnblogs.com/firecode/p/2647663.html
Copyright © 2011-2022 走看看