1

我使用这个方法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 与列表视图的滚动运动不兼容,但我对此无能为力。

真的希望有人可以帮助我!

4

1 回答 1

1

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

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

于 2012-07-20T06:31:49.137 回答