安卓之listview和textview爭搶焦點的解決辦法

來源:互聯網
上載者:User

標籤:焦點爭搶

     尋找了網上很多文章,說是在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爭搶焦點的解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.