在 EditText Android 中启用滚动条

编程入门 行业动态 更新时间:2024-10-27 01:23:41
本文介绍了在 EditText Android 中启用滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的布局中有一个 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 03:26:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:滚动条   EditText   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!