如何在android中滚动自定义edittext

时间:2012-10-03 05:27:41

标签: android android-edittext

我有一个自定义的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);
}

请帮帮我 谢谢

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你需要在1个布局中添加2个scrollView(正如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;
    }
});