Android EditText在ScrollView中不可滚动
我有一个Android EditText在ScrollView中不可滚动,android,android-layout,android-edittext,android-view,android-scrollview,Android,Android Layout,Android Edittext,Android View,Android Scrollview,我有一个滚动视图,里面有一个编辑文本,设置为垂直滚动。但它不会滚动。相反,每当我尝试滚动编辑文本时,整个布局都会滚动。 下面是代码- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:layout
滚动视图
,里面有一个编辑文本
,设置为垂直滚动。但它不会滚动。相反,每当我尝试滚动编辑文本时,整个布局都会滚动。
下面是代码-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="39dp"
android:text="Title"
android:textColor="#3bb9ff"
android:textSize="15sp" />
<EditText
android:id="@+id/Text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Title"
android:singleLine="true" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content"
android:layout_marginTop="50dp"
android:textColor="#3bb9ff"
android:textSize="15sp"
/>
<EditText
android:id="@+id/newTodoText"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:minLines="2"
android:maxLines="7"
android:hint="Write something"
android:scrollbars = "vertical" >
</EditText>
<Button
android:id="@+id/Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add" />
</LinearLayout>
</ScrollView>
id为“newTodoText”的EditText在这里有问题
- 您已将
ScrollView
设置为您的父most布局。这就是原因
你的整个布局都会滚动
- 就EditText而言,您已经使用了
android:scrollbars=“vertical”
这意味着如果编辑文本
将出现一个垂直滚动条。您将获得滚动条和滚动效果
仅当edittext具有足够高的数据时
希望这有帮助…您只需将
替换为类似
从代码中删除。它可以正常工作您已将“滚动视图”设置为您的父级-这意味着它可以滚动整个布局
如果你想滚动一个特定的“编辑文本”,那么你必须将另一个滚动视图放到该编辑文本上
EditText EtOne = (EditText) findViewById(R.id.EditText01);
EtOne.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.EditText01) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
//使用编辑文本对象更改hear mEdtText1,也使用滚动视图对象更改mScrlMain。它们确实有效。您应该使用NestedScrollView类。此类支持父滚动中的子滚动。此类可以是子类或父类
noteEdit.setMovementMethod(new ScrollingMovementMethod());
ScrollingMovementMethod.getInstance();
noteEdit.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
noteEdit.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d6d8d9">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="512"
android:text=" your content"/>
<android.support.v4.widget.NestedScrollView
android:layout_below="@id/ll_button"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#d6d8d9">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="your content"
android:maxLines="512"/>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
下面介绍了一种简单的方法
myEditText.setOnTouchListener(new View.OnTouchListener() { //Register a callback to be invoked when a touch event is sent to this view.
@Override
public boolean onTouch(View v, MotionEvent event) {
//Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.
mScrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
});
将此代码用于其工作状态
et_feedback.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (view.getId() == R.id.et_feedback) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
在Xml中
android:singleLine="false"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
在Pragraming中
et_feedback.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (view.getId() == R.id.et_feedback) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
XML代码
android:gravity="top"
android:inputType="text|textMultiLine"
android:lines="5"
对Kotlin使用以下代码
editText.setOnTouchListener { v, event ->
if (v.id == R.id.editText) {
v.parent.requestDisallowInterceptTouchEvent(true)
when (event.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_UP -> v.parent.requestDisallowInterceptTouchEvent(false)
}
}
false
}
我在滑动关闭的底部板材中遇到了类似的问题,但我怀疑这通常会起作用:
@SuppressLint("ClickableViewAccessibility")
fun View.blockAllAncestorsInterceptingTouch() {
setOnTouchListener { _, _ ->
parent?.requestDisallowInterceptTouchEvent(true)
false
}
}
suppress Lint是合法的,如果总是返回false,则无需调用performClick。即使数据足够高,EditText也不可滚动。如何使其可滚动?与我尝试的片段视图相同。它不起作用。有什么建议吗?抱歉。。。但是这个答案花了我一个小时正确的答案是:查找:Ayman Mahgoub AnswerAs@Shubh评论这不适用于fragment,这也不适用于imageview和button。这只是在使用textview。可能重复的是真的。。这段代码有效。。简单明了…很棒的Amsheer…非常感谢你回答一个问题,是否可以只注册此触摸截取(滚动编辑文本)当编辑文本中有足够的内容可以滚动时?@Jack.Ramsden在这种情况下,您可以将整个内容包装在onTouch中,if条件中的return语句除外,它可以获取大于一定数量字符(例如50个)的EditText长度结束检查.是否有必要在行动开始时删除不允许的拦截?我认为这是在手势结束时自然重置的。这对我来说非常有效。我无法让阿姆希尔的工作。非常感谢。我喜欢这种干净整洁的方式。我觉得你写的东西有点不对劲。没有使用ScrollingMovementMethod.getInstance()
。其他答案都不起作用,但您的答案起作用了。非常感谢。
android:gravity="top"
android:inputType="text|textMultiLine"
android:lines="5"
editText.setOnTouchListener { v, event ->
if (v.id == R.id.editText) {
v.parent.requestDisallowInterceptTouchEvent(true)
when (event.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_UP -> v.parent.requestDisallowInterceptTouchEvent(false)
}
}
false
}
@SuppressLint("ClickableViewAccessibility")
fun View.blockAllAncestorsInterceptingTouch() {
setOnTouchListener { _, _ ->
parent?.requestDisallowInterceptTouchEvent(true)
false
}
}