安卓之listview和textview争抢焦点的解决办法

     查找了网上很多文章,说是在XML文件设置textview的focus为false ,简直是一派胡言,那样连文本都输入不了了,有何用

网上查找无果,改用java代码控制listview的longclick方法,实现弹出复制和删除listview的办法

自定义一个popWindow 

private void initPopupWindow(LayoutInflater inflater) {
View view = inflater.inflate(R.layout.pop_item_layout, null);
popupWindow = new PopupWindow(view, 100, 50);
// popupWindow.setWidth(LayoutParams.WRAP_CONTENT);                  
// popupWindow.setHeight(LayoutParams.WRAP_CONTENT); 
copyTv = (TextView) view.findViewById(R.id.pop_copy_tv);
deleteTv = (TextView) view.findViewById(R.id.pop_delete_tv);
}


然后自己写一个监听方法

class tvOnTouch implements OnTouchListener {
private Context mContext;
private int mPosition;


public tvOnTouch(Context context, int position) {
// TODO Auto-generated method stub
this.mContext = context;
this.mPosition = position;
}


@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (v.getId() == R.id.pop_copy_tv) {
TextView tv = (TextView) v;
if (event.getAction() == MotionEvent.ACTION_DOWN) {// 按下
tv.setTextColor(0xff00CD66);
} else if (event.getAction() == MotionEvent.ACTION_UP) {// 弹起
tv.setTextColor(0xffffffff);

com.robot.utils.TextManager.copyText(mContext, mDatas.get(mPosition).getMsg());
Toast.makeText(mContext, "复制成功", SHOW_TIME).show();


if (popupWindow != null) {
popupWindow.dismiss();
}
}
} else {
TextView tv = (TextView) v;
if (event.getAction() == MotionEvent.ACTION_DOWN) {// 按下
tv.setTextColor(0xff00CD66);
} else if (event.getAction() == MotionEvent.ACTION_UP) {// 谈起
tv.setTextColor(0xffffffff);
mDatas.remove(mPosition);
notifyDataSetChanged();
Toast.makeText(mContext, "删除成功", SHOW_TIME).show();

if (popupWindow != null) {
popupWindow.dismiss();
}
}
}
return true;
}


}



最后在getview里加入监听事件

viewHolder.content.setOnLongClickListener(new OnLongClickListener() {


@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
showPop(v);
copyTv.setOnTouchListener(new tvOnTouch(mContext, position));
deleteTv.setOnTouchListener(new tvOnTouch(mContext, position));
return false;
}
});

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值