监听 TextView 的 drawableTop
Listener to drawableTop of TextView
需要处理 drawableTop
of 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(event.getAction() == MotionEvent.ACTION_UP)
中的代码没有被执行。如果我在 if 语句中写错了条件,请纠正我。
更改以处理 drawableTop 上的事件:
1) from if(event.getAction() == MotionEvent.ACTION_UP)
to
if(event.getAction() == MotionEvent.ACTION_DOWN)
2)from if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop())
to
if(event.getRawY() >= tvPhone.getTop() -
tvPhone.getTotalPaddingTop())
3) return 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;
}
});
需要处理 drawableTop
of 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(event.getAction() == MotionEvent.ACTION_UP)
中的代码没有被执行。如果我在 if 语句中写错了条件,请纠正我。
更改以处理 drawableTop 上的事件:
1) from
if(event.getAction() == MotionEvent.ACTION_UP)
toif(event.getAction() == MotionEvent.ACTION_DOWN)
2)from
if(event.getRawX() <= tvSocialMedia.getTotalPaddingTop())
toif(event.getRawY() >= tvPhone.getTop() - tvPhone.getTotalPaddingTop())
3) return 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;
}
});