在 TextView 和 EditText 之间切换
是否可以互换 TextView 和 EditText。就像在需要时显示文本,但在需要时允许编辑。是否有一种方法(如非 XML 中)编辑 TextView 或不编辑 EditText 的方法?
Is this possible to interchange a TextView and an EditText. Like display the text when needed, but allow editing when needed. Is there a method (as in NON-XML) way of editing a TextView or non-editing a EditText?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将两者放在布局中的同一位置。使一个不可见。根据需要切换可见性。
或者,将两者作为
ViewSwitcher
或ViewFlipper
的子级,并使用它们在它们之间进行切换。不,据我所知。
Put both in your layout in the same spot. Make one invisible. Toggle visibility as needed.
Or, put both as children of a
ViewSwitcher
orViewFlipper
, and use that to switch between them.No, AFAIK.
是的,事实上,根据
EditText
代码:而且它非常很薄!您基本上可以说
TextView
是一个配置为不可编辑的EditText
。EditText
的代码非常短,我将其放在这个问题的底部以供参考。唯一真正的区别在于样式,它们是在 XML 中设置的:
您可以使用以下内容将它们重置为 java 中的
TextView
值:您可能不想更改所有这些,例如也许留下重力和文本颜色/外观,所以事情不会突然发生奇怪的变化。
EditText
代码Yes, in fact, according to the
EditText
code:And it is very thin! You could basically say a
TextView
is anEditText
that is configured to be non-editable. The code forEditText
is so short I put it at the bottom of this question for reference.The only real difference is in the styles, which are set in XML:
You can reset those to the
TextView
values in java using something like this:You probably won't want to change all of those, e.g. maybe leave gravity and text colour/appearance, so things don't suddenly change weirdly.
EditText
codeEditText
是TextView
的子类。如果您想禁用EditText
的编辑功能,只需将inputType
设置为"none"
,它将表现为>文本视图
。EditText
is a subclass ofTextView
. If you want to disable the editing function of anEditText
you can simply set theinputType
to"none"
and it will behave as aTextView
.我创建了一些方法,您可以在需要时利用它们进行适当的转变。希望这对大家有帮助。
您可以利用这些方法在 EditText 以及 AppCompatEditText (Androidx) 的查看和编辑模式之间切换。
I have created a few methods which you can leverage to do the appropriate shift whenever needed. Hope This will Help everyone.
You can leverage these methods to Switch Between Viewing and Editing Mode For EditText as well As AppCompatEditText (Androidx).