简体   繁体   English

侦听器到TextView的drawableTop

[英]Listener to drawableTop of TextView

Need to handle click event on drawableTop of TextView , written following code, 需要处理TextView drawableTop的click事件,代码如下,

    tvSocialMedia.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP) {
                if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop()) {
                    // your action for drawable click event
                    tvEmail.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.email,0, 0);
                    tvPhone.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.phone,0, 0);
                    tvAddress.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.address,0, 0);
                    tvSocialMedia.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.social_media_select,0, 0);
                    usage.setText(getResources().getString(R.string.social_media));
                    return true;
                }
            }
            return true;
        }
    });

But code inside if(event.getAction() == MotionEvent.ACTION_UP) is not getting executed. 但是if(event.getAction() == MotionEvent.ACTION_UP)内部的代码未执行。 Correct me if I have written wrong condition in if statement. 如果我在if语句中写错了条件,请纠正我。

changes to handle event on drawableTop: 更改以处理drawableTop上的事件:

1) from if(event.getAction() == MotionEvent.ACTION_UP) to if(event.getAction() == MotionEvent.ACTION_DOWN) 1)从if(event.getAction() == MotionEvent.ACTION_UP)if(event.getAction() == MotionEvent.ACTION_DOWN)

2)from if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop()) to if(event.getRawY() >= tvPhone.getTop() - tvPhone.getTotalPaddingTop()) 2)从if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop())if(event.getRawY() >= tvPhone.getTop() - tvPhone.getTotalPaddingTop())

3) return false 3)返回假

tvPhone.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                if(event.getRawY() >= tvPhone.getTop() - tvPhone.getTotalPaddingTop()) {
                    // your action for drawable click event
                    tvEmail.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.email,0, 0);
                    tvPhone.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.phone_select,0, 0);
                    tvAddress.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.address,0, 0);
                    tvSocialMedia.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.social_media,0, 0);
                    usage.setText(getResources().getString(R.string.phone_no));
                    return true;
                }
            }
            return false;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM