1. 程式人生 > >解決listview裡面edittext焦點問題

解決listview裡面edittext焦點問題

由於每次彈起輸入框,輸入法,listview的介面都會重置佈局

導致edittext失去焦點,或者焦點錯亂

所以我們必須在listview裡面對edittext特殊處理

由於重置佈局,必須走getview裡面的程式碼,我們可以在getview裡面標記游標位置,在getview結束前重新設定焦點

問題完美解決

//設定edittext的輸入改變監聽    
 vh.guige_kuncun_item.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
public void onFocusChange
(View v, boolean hasFocus) { if (hasFocus){ vh.guige_kuncun_item.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s.length() == 0) { guigelist.get(position
).setGuige_kucun(0); ShowToast("請設定規格庫存!"); } else guigelist.get(position).setGuige_kucun(Integer.parseInt(s.toString())); index=position; } }); } } }); // vh.guige_price_item.addTextChangedListener(new MyTextWatcher(vh,0)); // vh.guige_kuncun_item.addTextChangedListener(new MyTextWatcher(vh,1));
記錄游標 焦點位置
vh.guige_kuncun_item.setOnTouchListener(new View.OnTouchListener() {

                @Override
public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
                        index = position;
priceitem=-1;
Log.i("0302", "onTouch"+index);
}
                    return false;
}
            });
vh.guige_price_item.setOnTouchListener(new View.OnTouchListener() {
                @Override
public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
                        priceitem = position;
index=-1;
Log.i("0302", "onTouch"+index);
}
                    return false;
}
            });
vh.guige_price_item.clearFocus();
vh.guige_kuncun_item.clearFocus();
重置焦點
            if (priceitem != -1 && priceitem== position) {
                // 如果當前的行下標和點選事件中儲存的index一致,手動為EditText設定焦點。
vh.guige_price_item.requestFocus();
}
            if (index != -1 && index == position) {
                // 如果當前的行下標和點選事件中儲存的index一致,手動為EditText設定焦點。
vh.guige_kuncun_item.requestFocus();
}
重置游標
            vh.guige_kuncun_item .setSelection(vh.guige_kuncun_item .getText().length());
vh.guige_price_item .setSelection(vh.guige_price_item .getText().length());