Android的:在设置里面onTouchListener为RelativeLayout的颜色 [英] Android: setting colors for relativelayout inside onTouchListener

查看:196
本文介绍了Android的:在设置里面onTouchListener为RelativeLayout的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些RelativeLayout的,其内部有多个TextView的和imagebuttons,所以为了不使 onTouchListener 一个接一个,我已经实现了低于code:

I am having some relativeLayout, inside which there are multiple textview and imagebuttons, so in order not to making onTouchListener one by one, I have implemented the below code:

    relative1.setOnTouchListener(new OnTouchListener() 
    {
        @Override
        public boolean onTouch(View arg0, MotionEvent event) 
        {
            if(event.getAction()==MotionEvent.ACTION_DOWN )
            {
                relative1.setBackgroundColor(getResources().getColor(R.color.tran_black));
            }

            if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
            {
                relative1.setBackgroundColor(getResources().getColor(android.R.color.transparent));
            }
            return false;
        }           
    }); 

问:

relative1 RelativeLayout的已转向 tran_black 颜色当pressed,但它不能在 Action_up 回头透明。

Question:

The relative1 RelativeLayout has turned to tran_black color when pressed, but it fails to turn back to transparent upon Action_up.

怎么可能ameneded?谢谢!

How could that be ameneded? Thanks!

推荐答案

就返回真正而不是

据<一个href=\"http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch%28android.view.View,%20android.view.MotionEvent%29\"相对=nofollow> onTouch方法DOC ,返回值为 True如果听者已经消耗的情况下,否则为false。
这意味着,当你返回false,连续的事件将不会被传递到您的收听。

According to onTouch method doc, the return value is True if the listener has consumed the event, false otherwise. This means that when you return false, successive events will not be passed to your listener.

您也可以参考我的答案<一个href=\"http://stackoverflow.com/questions/17291027/ontouch-events-not-working-properly/17291426#17291426\">here其中有意见的例子。

You can also refer my answer here which has example in comments.

这篇关于Android的:在设置里面onTouchListener为RelativeLayout的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆