make a cursor focus to edittext android

I have an android listview with thousands of items. Each Item inside its just contains an EditText with a default of text.

After the listview loading data successfull. I click to one of item - at first time, the cursor focus to the selected EditText > The soft input keyboard appear > The keyboard push the listview scroll up and the selected EditText lost its focus! Since the keyboard were shown on screen, this will not happen any more. I don't know

I have checked it by this event:

setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
}
}

So the question here is how can I re-focus or make a focus of my cursor to the exactly position after I lost my focus before


I think You should request focus in Your EditText onClickListener.

someEditText.setOnClickListener(new OnClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            someEditText.requestFocus();                                
        }
    });

But without seeing Your code i'm just guessing.


I have my own solution after research for weeks but very busy to update for everyone know.

The solution is created a custom Scrollview in order to cover the listview of edittexts .

In the Scrollview, override event like this. That's it.

With this one, in the future if we want our Edittext scrolls smooth or control the focusing

public class VerticalScrollview extends ScrollView {

    public VerticalScrollview(Context context) {
        super(context);
        init();
    }

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

    public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        //setFillViewport(true);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        onTouchEvent(ev);
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false");
                break;

            case MotionEvent.ACTION_MOVE:
                return false; // redirect MotionEvents to ourself

            case MotionEvent.ACTION_CANCEL:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                super.onTouchEvent(ev);
                break;

            case MotionEvent.ACTION_UP:
                Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                return false;

            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
        }

        return false;

//        onTouchEvent(ev);
//        float startY=0;
//        if (ev.getAction() == MotionEvent.ACTION_DOWN) startY = ev.getY();
//        return (ev.getAction() == MotionEvent.ACTION_DOWN) && (Math.abs(startY - ev.getY()) > 50);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction());
        return true;
    }
}
链接地址: http://www.djcxy.com/p/4146.html

上一篇: 停止使用活动中的滚动视图获得焦点的edittext

下一篇: 使光标焦点来编辑文本android