1

我有一个自定义的 EditText,它在其触摸事件中编写了显示键盘视图的代码。现在我无法滚动它,并且单击它无法将光标定位在触摸位置。我找到了在触摸位置定位光标的代码,它可以工作仅当 edittext 不滚动时才正确。

代码是:

Layout layout = ((EditText) text).getLayout();
if (layout != null)
{
    int line = layout.getLineForVertical((int) event.getY());
    int offset = layout.getOffsetForHorizontal(line, event.getX());
    ((EditText)text).setSelection(offset);
}

请帮帮我谢谢

4

2 回答 2

0

您需要将 EditText 包含在 ScrollView 中以启用滚动

于 2012-10-03T05:52:16.387 回答
0

您需要在 1 个布局中添加 2 个滚动视图(如 Royston 所说)(结果是在某些手机上它看起来很滞后)这是一些代码片段:首先在 xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myparentScrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ScrollView
    android:id="@+id/mychildScrollView"

        <TextView
        / >


</ScrollView>

并将其添加到后面的代码中

 parentScrollView= (ScrollView) findViewById(R.id.myparentScrollview);
parentScrollView.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
         //Log.v(TAG,"PARENT TOUCH");
         findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false);
        return false;
        }
    });
childScrollView= (ScrollView) findViewById(R.id.childScrollView);
childScrollView.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
         //Log.v(TAG,"PARENT TOUCH");
         findViewById(R.id.mychildScrollView).getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});
于 2012-10-03T06:13:54.510 回答