Scrolling EditText inside ScrollView(在 ScrollView 内滚动 EditText)
问题描述
我有一个讨厌的问题.我在 ScrollView
中有 EditText
(8 行).当我试图在 EditText
中滚动文本时,它的行为并不稳定.有时它在滚动,有时它没有集中注意力.
I have a nasty problem. I have EditText
(8 lines) inside ScrollView
. And when I'm trying to scroll text in EditText
it's behavior is not stable. Sometimes it's scrolling, some times it's not taking focus.
这是我的布局文件,让我的问题更清楚:
This is my layout file, to make my question more clear:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="@+id/wo_task_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_description" />
<TextView
android:id="@+id/wo_task_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_description" />
<TextView
android:id="@+id/wo_task_comments_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/comments" />
<Button
android:id="@+id/wo_task_select_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/select_comment_from_template" />
<EditText
android:id="@+id/wo_task_comments"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:hint="@string/enter_your_comment_here"
android:lines="8"
android:maxLines="10"
android:minLines="6"
android:scrollbars="vertical"
android:singleLine="false" />
</LinearLayout>
我知道我遇到这个问题是因为将一个可滚动控件放在另一个控件中,但我不知道该怎么做.所以,如果可以的话,请帮助我.
I understand that I'm having this issue because of holding one scrollable control inside another, yet I don't know what to do with this. So, please help me if you can.
提前致谢.
public void initComments(final View view) {
EditText comment = (EditText) view.findViewById(R.id.wo_task_comments);
comment.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(final View v, final MotionEvent motionEvent) {
if (view.getId() == R.id.wo_task_comments) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(
false);
break;
}
}
return false;
}
});
comment.setText(currentTask.getComment() + "very very long comment"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment");
}
我试过了,没有结果.我仍然无法滚动我的编辑框.
I've tried this, and no result. I'm still unable to scroll my editbox.
推荐答案
试试:
@Override
public void onCreate(Bundle savedInstanceState) {
.....
EditText et = (EditText)findViewById(R.id.wo_task_comments);
et.setOnTouchListener(this);
.....
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(view.getId() == R.id.wo_task_comments){
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
在你的情况下:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initComments(findViewById(R.id.YOUR_MAIN_LAYOUT_ID));
}
public void initComments(final View view) {
EditText comment = (EditText) view.findViewById(R.id.wo_task_comments);
comment.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View v, final MotionEvent motionEvent) {
if (v.getId() == R.id.wo_task_comments) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(
false);
break;
}
}
return false;
}
});
comment.setText("very very long comment"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment
" + "very very long comment
"
+ "very very long comment");
}
}
这篇关于在 ScrollView 内滚动 EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 ScrollView 内滚动 EditText
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01