安卓之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;
}
});

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。