双击textView

时间:2011-12-26 13:34:04

标签: android textview ontouchlistener

enter image description here例如,

Welcome to Android World.

现在当我双击“欢迎”和“到”之间的空格时,从“到”到“世界”的字符串应该出现在下一行。

即,

Welcome <\n>
to Android World.

同样,当我双击“to”和“Android”之间的空格时,它应该是,

Welcome <\n>
to <\n>
Android World.

它第一次工作,但下一次强制停止。 我不知道我哪里出错了。可能没有正确获取onTouchListener。

需要帮助。

linear_layout = (LinearLayout) findViewById(R.id.linearLayout);

    mTextView =  new TextView[10];
    mTextView[i] = new TextView(this);
    mTextView[i].setText("Hello Android Text View");
   linear_layout.addView(mTextView[i]);

    mTextView[i].setOnTouchListener(this);


    mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
        @Override
        public void onLongPress(MotionEvent e) {
            Log.d(TAG, "Long Press event");
            Toast.makeText(getBaseContext(), "Long Press", Toast.LENGTH_LONG).show();
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.d(TAG, "Double Tap event");

            Toast.makeText(getBaseContext(), "Double Tap", Toast.LENGTH_LONG).show();

            Log.i("Tag", "------------------------------ " + e.getX() + "      " + e.getY());

            Layout layout = ((TextView) view).getLayout();
            int x = (int)e.getX();
            int y = (int)e.getY();
            if (layout!=null){
                line = layout.getLineForVertical(y);
                characterOffset = layout.getOffsetForHorizontal(line, x);
                Log.i("index", ""+characterOffset);
            }

            String text = mTextView[i].getText().toString();
            char[] char_txt = text.toCharArray();
            int ascii_val = (int)text.charAt(characterOffset);
            String rem_txt = "";

            //if(ascii_val == 32) {
                int n=characterOffset;

                while(n < char_txt.length){
                    rem_txt += char_txt[n];
                    n++;
                }
            //}

            i++;
            String before_tap_txt = text.subSequence(0, characterOffset).toString();
            mTextView[i-1].setText(before_tap_txt);
            mTextView[i] = new TextView(GestureDetecterExampleActivity.this);
            mTextView[i].setText(rem_txt);
            linear_layout.addView(mTextView[i]);

            return true;

        }
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    });
    mGestureDetector.setIsLongpressEnabled(true);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    view = v;
    return mGestureDetector.onTouchEvent(event);

}

1 个答案:

答案 0 :(得分:0)

                    Log.d(TAG, "Double Tap event");

                /*  Toast.makeText(getBaseContext(), "Double Tap",
                            Toast.LENGTH_LONG).show();*/

                    Log.i("Tag",
                            "------------------------------ " + e.getX()
                                    + "      " + e.getY());

                    Layout layout = ((TextView) view).getLayout();
                    int x = (int) e.getX();
                    int y = (int) e.getY();
                    if (layout != null) {
                        line = layout.getLineForVertical(y);
                        characterOffset = layout.getOffsetForHorizontal(
                                line, x);
                        Log.i("index", "" + characterOffset);
                    }

                    String text = mTextView[i].getText().toString();

                    char[] char_txt = text.toCharArray();

                    int ascii_val = (int) text.charAt(characterOffset);
                    String rem_txt = "";

                    if(ascii_val == 32) {
                        int n = characterOffset;

                        while (n < char_txt.length) {
                            rem_txt += char_txt[n];
                            n++;
                        }
                        // }

                        i++;
                        String before_tap_txt = text.subSequence(0,
                                characterOffset).toString();
                        mTextView[i - 1].setText(before_tap_txt.trim());
                        mTextView[i] = new TextView(
                                DoubleTapActivity.this);
                        mTextView[i].setText(rem_txt.trim());
                        mTextView[i].setOnTouchListener(DoubleTapActivity.this);
                        linear_layout.addView(mTextView[i]);

                        return true;

                    }
                    else
                    {
                        Toast.makeText(DoubleTapActivity.this, "" + text.charAt(characterOffset), Toast
                                .LENGTH_SHORT).show();
                    }
                    return false;

将此代码放在双击方法中。 如果您需要任何帮助,请发表评论。