监听textable的drawableTop

时间:2017-01-24 06:30:47

标签: android textview android-drawable ontouchlistener

需要处理drawableTop的{​​{1}}点击事件,代码编写,

TextView

但是 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; } }); 内的代码没有被执行。如果我在if语句中写错了条件,请纠正我。

1 个答案:

答案 0 :(得分:0)

更改处理drawableTop上的事件:

  

1)从if(event.getAction() == MotionEvent.ACTION_UP)到。{   if(event.getAction() == MotionEvent.ACTION_DOWN)

     

2)从if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop())到。{   if(event.getRawY() >= tvPhone.getTop() - tvPhone.getTotalPaddingTop())

     

3)返回false

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;
        }
    });