>我正在制作一个电子商务应用程序,其购物车列表具有由EditText
组成的自定义ListView
。EditText
表示物料的数量。我正在使用OnFocusChangeListener
来检测客户何时完成更改商品数量,然后更新服务器上的购物车。一切正常,只是onFocusChange
被调用了两次,即我得到了两次false
。
viewHolder.etProductQuantity.setOnFocusChangeListener( new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus){
// Updating the ProductList class's object to set the new quantity
// Updating product quantity on server
Log.d("Product Quantity", viewHolder.etProductQuantity.getText().toString() + b);
}
}
});
因此,编码被执行两次,这会产生问题。
将
以下行添加到清单中的活动中可解决此问题:
android:windowSoftInputMode="adjustPan"
不知道为什么。
尝试维护一个检查丢失焦点的标志 代码执行一次然后再也不执行:
viewHolder.etProductQuantity.setTag(1);
viewHolder.etProductQuantity.setOnFocusChangeListener( new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus && ((Integer)view.getTag())==1){
// Updating the ProductList class's object to set the new quantity
// Updating product quantity on server
Log.d("Product Quantity",
viewHolder.etProductQuantity.getText().toString() + b);
view.setTag(0);
}else{
view.setTag(1);
}
}
});
你可以这样使用:
edt_sys_log_search.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do calling WS
}
return false;
}
});
并按如下所示设置编辑文本属性:
android:imeOptions="actionDone"
singleLine="true"
当用户从软键盘按完成时,它将调用 WS