您的位置:首页 > 其它

ListView+EditText失去焦点和软键盘问题解决办法

2014-11-06 16:54 796 查看
问题:

EditText要求输入数字,点击之后,getview()这个方法再次被执行,软键盘从数字很快变成普通键盘,并且失去焦点。

原因:

软键盘弹出式,listview被重新绘制。

解决方法:

1. 重新绘制之前,记录下焦点位置;

2. 绘制完成之后,重新获取焦点;

栗子(项目中用到的):

public class OrderTotalDetailListAdapter extends BaseAdapter {
private ArrayList<OrderTotal> orderList = new ArrayList<OrderTotal>();

public OrderTotalDetailListAdapter(int product_id) {
orderList = OrderTotalDAO.getOrderTotalList(product_id);
}

@Override
public int getCount() {
return orderList.size();
}

@Override
public Object getItem(int position) {
return orderList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
OrderTotal orderTotal = (OrderTotal) getItem(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.order_detail_4_sine_item, null);
}
((OrderTotalDetailView)convertView).setValue(orderTotal, position);
return convertView;
}

}


public class OrderTotalDetailView extends LinearLayout {
private EditText real_volume;
private OrderTotal orderTotal;

private int index = -1;

public OrderTotalDetailView(Context context) {
super(context);
}

public OrderTotalDetailView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@SuppressLint("ClickableViewAccessibility")
public void setValue(OrderTotal _orderTotal, final int position) {
this.orderTotal = _orderTotal;
if (real_volume == null) {
real_volume = (EditText) findViewById(R.id.real_volume);
}
pop_name.setText(orderTotal.getPop());
pred_volume.setText(orderTotal.getPred_volume() + "");
real_volume.setText(orderTotal.getReal_volume()  + "");

real_volume.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// 在TOUCH的UP事件中,要保存当前的行下标,因为弹出软键盘后,整个画面会被重画
// 在getView方法的最后,要根据index和当前的行下标手动为EditText设置焦点
index = position;
}
return false;
}
});

real_volume.addTextChangedListener(new TextWatcher() {

private String preValue = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (preValue.equals(s.toString())) {
return;
}
orderTotal.setReal_volume(Integer.parseInt(s.toString()));
OrderTotalDAO.updateOrdersTotalVolume(orderTotal);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
preValue = s.toString();
}

@Override
public void afterTextChanged(Editable s) {

}
});

real_volume.clearFocus();
// 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
if(index!= -1 && index == position) {
real_volume.requestFocus();
}
// 焦点移到最后
real_volume.setSelection(real_volume.getText().length());
}

}


参考:
http://blog.csdn.net/l_serein/article/details/7517011 http://www.tuicool.com/articles/MNFFna
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐