码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-04-13 20:59:58      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:焦点争抢

     查找了网上很多文章,说是在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;
}
});

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

标签:焦点争抢

原文地址:http://blog.csdn.net/charlist/article/details/45030259

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!