无法在列表视图项目中正确选择文本

时间:2012-07-20 06:08:55

标签: android listview android-edittext selection

我使用了这个方法, android.text.Selection.setSelection(Spannable text,int index),在MyEditText中选择一些文本,自定义类扩展EditText,同时移动我的手指。它在单个视图中完美地工作,但是当我将它应用于ListView中的多个MyEditText项时,它无法正常工作。

以下是我的代码,你也可以从github(https://github.com/ruobin/unconstrained-selecting-texts)下载:

MyEditText.java

    public class MyEditText extends EditText {

    public MyEditText(Context context) {
        super(context);
        initialize();
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

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

    private void initialize() {
        setBackgroundColor(Color.WHITE);
    }

    @Override
    protected void onCreateContextMenu(ContextMenu menu) {
        // avoid to pop out the context menu
    }

    @Override
    public boolean getDefaultEditable() {
        return false;
    }

    private int offset;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        Layout layout = getLayout();
        int line = 0;
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            line = layout.getLineForVertical(getScrollY() + (int) event.getY());
            offset = layout.getOffsetForHorizontal(line, (int) event.getX());
            Selection.setSelection(getEditableText(), offset);
            break;
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
            line = layout.getLineForVertical(getScrollY() + (int) event.getY());
            int curOff = layout
                    .getOffsetForHorizontal(line, (int) event.getX());
            Selection.setSelection(getEditableText(), offset, curOff);
            break;
        }
        return true;
    }

}

MyListAdapter.java

public class MyListAdapter extends BaseAdapter {

    private ArrayList<String> entitys;
    private Context context;

    public MyListAdapter(Context context, ArrayList<String> entitys) {
        this.context = context;
        this.entitys = entitys;
    }

    @Override
    public int getCount() {
        if (entitys == null)
            return 0;
        return entitys.size();
    }

    @Override
    public Object getItem(int position) {
        if (entitys == null)
            return null;
        return entitys.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String entity = entitys.get(position);
        MyEditText myTextView = (MyEditText) View.inflate(context,
                R.layout.list_item, null);
        myTextView.setText(entity);
        return myTextView;
    }

}

CopyTextTest2Activity.java

public class CopyTextTest2Activity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ListView lv = new ListView(this);
        String values;

        ArrayList<String> stringList = new ArrayList();
        for (int i = 0; i < 10; i++) {
            values = "" + i + "  sdfsfjskldfhsklhskdfsdhf"
                    + "fsdfaskhfkshdfkskdfskldf"
                    + "dfskdfhkshdfkshdkiuiouweoruweoruwoeruwoer"
                    + "opkspdfmmbb" + "" + "dsfasdfsdfsdfsdferwre";
            stringList.add(values);
        }

        MyListAdapter myAdapter = new MyListAdapter(this, stringList);
        lv.setAdapter(myAdapter);
        lv.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

        ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        // lv.setEnabled(false);
        setContentView(ll);

    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<ss.test.MyEditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

当你运行这个项目时,你会发现只有列表视图中的第一个项目是可选择的,而另一个奇怪的事情是你不能选择你圈出的所有文本。

我的观点是onTouchEvent与listview的滚动运动不兼容,但我无能为力。

真的希望有人能帮助我!

1 个答案:

答案 0 :(得分:1)

  

我的观点是onTouchEvent与滚动不兼容   列表视图的移动,但我无能为力。

要解决此问题,请覆盖Activity.dispatchTouchEvent()(http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)并将其传递给ListView,最后传递给View。让它先做它的工作。然后让View系统完成它的工作。