简体   繁体   English

安卓 EditText中的图像

[英]Android. Image in EditText

I wanna create a simple calculator. 我想创建一个简单的计算器。 One from my goals its place in EditText field images with special math chars as like square root, pi... Fe : 从我的目标中可以看出,它在EditText字段图像中的位置具有特殊的数学字符,例如平方根,π... Fe:

2/34*sqrt(121)

Here I want to place images instead of divide, multiply chars, sqrt string. 在这里,我要放置图像而不是除法,乘以字符,sqlt字符串。 how I can do it? 我该怎么办?

Some more questions: Does it actually need to be Editable ie user can type directly into it? 还有其他问题:它实际上是否需要可编辑,即用户可以直接在其中输入内容? If so how will it work? 如果是这样,它将如何运作? when they type '*' it will realize it and replace it with your png? 当他们输入“ *”时,它将意识到并用您的png替换它? Would it be acceptable if it didn't interrupt as they type but instead all at once when they are finished entering the entire line? 如果输入时不打断,而是在输入完整行时立即全部打断,这是否可以接受?

Best answer I have for now: EditText doesn't support that by default. 我目前的最佳答案:默认情况下,EditText不支持该功能。 You'll likely have to create your own view that is some kind of mashup of EditText and LinearLayout that can hold ImageViews with your pngs. 您可能必须创建自己的视图,该视图是某种EditText和LinearLayout的混搭,可以将ImageViews与PNG一起保存。 If it doesn't need to be editable by the user it is going to be easier because you can make your custom view a LinearLayout and add TextView's and ImageViews with the proper text and images set in them. 如果不需要用户对其进行编辑,它将变得更加容易,因为您可以使自定义视图成为LinearLayout,并添加TextView和ImageView,并在其中设置适当的文本和图像。 If it does need to be editable it is going to a bigger challenge. 如果确实需要编辑,它将面临更大的挑战。 You'll have to use a TextChangedListener to pull out each character as it is typed and replace it with an ImageView if need be. 您将必须使用TextChangedListener提取键入的每个字符,并在需要时将其替换为ImageView。 Seems to me like the hard part is going to be knowing where to position the ImageView and having the EditText know that you want it to pick back up with text on the other side of the Image. 在我看来,最困难的部分是要知道将ImageView放置在哪里,并使EditText知道您希望它与Image另一侧的文本一起显示。 Im not sure how you'd go about that. 我不确定您会怎么做。 Maybe the best solution is you have an EditText that they type into and then seperately you have your custom view as a display view. 也许最好的解决方案是您输入一个EditText,然后分别将您的自定义视图作为显示视图。 That way you don't have to worry about positioning images over the top of an EditText and correctly moving the cursor to right side of the image. 这样,您不必担心将图像放置在EditText上方并将光标正确移动到图像的右侧。

What I would suggest is looking into a freely available font that includes mathematical symbols (some quick searching turned up the StixFont project ), then just add that font to your assets, and set it as your typeface. 我的建议是研究一种包含数学符号的免费字体(一些快速搜索打开了StixFont项目 ),然后将其添加到资产中,并将其设置为字体。 You'll still probably need a TextWatcher/TextChangedListener as Tim suggested (unless you're using your own keyboard to input the symbols) but this would get around the problem of trying to insert images into a text field, and would also keep the vector quality provided by fonts versus a raster image embedded into a text field. 正如Tim所建议的那样,您可能仍将需要一个TextWatcher / TextChangedListener(除非您使用自己的键盘来输入符号),但这可以避免尝试在文本字段中插入图像的问题,并且可以保留矢量字体提供的质量与嵌入文本字段中的光栅图像相比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM