I wanna create a simple calculator. One from m开发者_如何学Cy goals its place in EditText field images with special math chars as like square root, pi... F.e. :
2/34*sqrt(121)
Here I want to place images instead of divide, multiply chars, sqrt string. how I can do it?
Some more questions: Does it actually need to be Editable i.e. 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? 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. 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. 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. 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. 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. 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. 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.
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. 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.
精彩评论