android 代码中控件触摸时添加背景

来源:互联网 发布:爱福窝软件下载 编辑:程序博客网 时间:2024/05/18 02:33

效果:


本效果是触摸事件实现代码实现:LinearLayout llt = new LinearLayout(this);llt.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {v.setBackgroundColor(Color.parseColor("#F5F5F5"));if(event.getAction() == MotionEvent.ACTION_UP)v.setBackgroundColor(Color.parseColor("#ffffff"));return false;}});PS: 屏幕背景为白色 LinearLayout为一个屏幕中的item,item边框黑灰色效果最佳。可根据自己的item色调进行配色,来达到最佳效果知识点: 1.setOnTouchListener 中的 onTouch 触发事件的各个行为状态 、坐标   MotionEvent Action 此效果用到  .ACTION_MOVE(间接用到默认) ,ACTION_UP 还有一些常用的 ACTION_DOWN、ACTION_CANCEL等等  MotionEvent event 获得触摸屏幕坐标位置 event.getX()、event.getY() 等等

原创粉丝点击