在 EditText Android 中启用滚动条 [英] Enabling scrollbar in EditText Android

查看:46
本文介绍了在 EditText Android 中启用滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局中有一个 EditText.以下是我目前拥有的属性:

I have an EditText on my layout. Below are the attributes I currently have:

<EditText
   android:id="@+id/entryIdea"
   android:layout_width="fill_parent"
   android:layout_height="225sp"
   android:gravity="top"
   android:background="@android:drawable/editbox_background"
   android:scrollbars="vertical"/>

但是,我可以看到滚动条,但无法使用鼠标/触摸滚动它.我认为如果我放置相应的侦听器,它可能会起作用,因为它适用于 TextView.显然不是.

However, I can see the scrollbar but can't scroll it with mouse/touch. I thought that it may works if I put the corresponding listener since it works on TextView. Apparently, it isn't.

EditText et = (EditText)findViewById(R.id.entryIdea);
et.setMovementMethod(new ScrollingMovementMethod());

你们能帮我解决这个问题吗?

Can you guys help me on this?

在此先感谢您.萨米

推荐答案

在你的 XML 中尝试设置 EditText 高度而不是在 layout_height 中,而是使用 android:lines 属性(顺便说一句,在设置字体大小以外的任何大小时,使用 sp 通常不是一个好习惯.在这种情况下使用 dp/dip 更自然).

In your XML try setting the EditText height not in layout_height, but rather use android:lines attribute (btw, using sp is not usually a good practice when setting a size for anything except font size. Using dp/dip is more natural in this case).

同时将 layout_height 设置为 wrap_content.否则,即使没有在代码中指定移动方法,您提供的 XML(带有我提到的更改)对我来说也能正常工作.

Meanwhile set layout_height to wrap_content. Otherwise the XML you presented (with the changes I've mentioned) worked fine for me even without specifying movement method in the code.

当然,当EditText中的实际文本行数大于android:lines属性时,滚动条就会出现.

And of course, the scroll bar will appear when the actual amount of lines of text in the EditText is larger than that, indicated in android:lines attribute.

这篇关于在 EditText Android 中启用滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆