android–onTouchListener覆盖Button视图上的自定义背景?

android–onTouchListener覆盖Button视图上的自定义背景?,第1张

概述我的res/drawable文件夹中有一个button_animation.xml,用于显示不同的按钮状态(默认,按下,聚焦).我在布局文件的按钮中引用了button_animation.xml.它完美地工作,除了我在实际被按下的按钮上设置onTouchListener.以下是我的代码.button_animation.xml<selectorxmlns:android=

我的res / drawable文件夹中有一个button_animation.xml,用于显示不同的按钮状态(默认,按下,聚焦).我在布局文件的按钮中引用了button_animation.xml.它完美地工作,除了我在实际被按下的按钮上设置ontouchListener.以下是我的代码.

button_animation.xml

<selector xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item androID:drawable="@drawable/button_pressed"          androID:state_pressed="true" />    <item androID:drawable="@drawable/button_focused"          androID:state_focused="true" />    <item androID:drawable="@drawable/button_default" /></selector>

layout.xml

     <button         androID:ID="@+ID/button1"         androID:layout_wIDth="wrap_content"         androID:layout_height="wrap_content"         androID:background="@drawable/button_animation" />

导致动画中断的代码

button button = (button) findVIEwByID(R.ID.button1);button.setontouchListener(this);

我是否无法按照文档建议显示按钮状态并同时处理任何特定视图的onClick?

文档:http://developer.android.com/guide/topics/ui/controls/button.html

谢谢,

贾森

解决方法:

您应该使用button.setonClickListener(this)而不是button.setontouchListener(this),该类应该实现OnClickListener.

如果您仍需要处理ontouch(向下和向上),您可以自己处理背景设置.

public boolean ontouch( VIEw button, MotionEvent theMotion ) {   switch ( theMotion.getAction() ) {      case MotionEvent.ACTION_DOWN:           //Set button background here          break;      case MotionEvent.ACTION_UP:           //set button to default background          break;   }    return true;}
总结

以上是内存溢出为你收集整理的android – onTouchListener覆盖Button视图上的自定义背景?全部内容,希望文章能够帮你解决android – onTouchListener覆盖Button视图上的自定义背景?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116043.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存