【发布时间】:2015-03-24 02:38:18
【问题描述】:
我正在开发一个 Android 应用程序,我需要能够将指数/幂包含到字符串 (Java) 中。我已经进行了一些搜索,似乎我的情况使它变得困难。我研究了诸如 ALT+ 代码和 Unicode 之类的选项。我不完全确定其中任何一个或任何其他选项是否真的可行(奇怪的是,我知道 ALT+ 代码只有 '1' '2' 和 '3' 下标)。
那么,如果有的话,我最好的选择是什么?
我想让它尽可能简单。我有很多字符串要写(比如数学问题),下标/上标数字的需要是随机的。这很容易做到吗?
编辑:我想我含糊其辞。我的应用程序用于学习。并且问题/答案每次都被放入数组中并随机化。我希望能够像现在一样在 Java 文件中设置字符串
question[0].text = "solve x^2 if x=5";
但我不想让用户看到“^2”,而是使用实际的上标。
另一个编辑:
global.java(保存问题/答案的类/结构)
question[0].question = "Question Text. I would like to add a superscript or two here, as if it were a math problem";
question[0].answer[0]. = "Answer 1. Add a subscript here";
question[0].answer[1]. = "Answer 2. Add another superscript here.";
question[0].answer[2]. = "Answer 3. Etc.";
question[0].answer[3]. = "Answer 4. Etc.";
活动 XML
<!--QUESTION-->
<TextView
android:id="@+id/textQuestion"/>
<!--ANSWERS-->
<Button
android:id="@+id/answerOne"
android:onClick="checkAnswer"/>
<Button
android:id="@+id/answerTwo"
android:onClick="checkAnswer"/>
<Button
android:id="@+id/answerThree"
android:onClick="checkAnswer"/>
<Button
android:id="@+id/answerFour"
android:onClick="checkAnswer"/>
活动 Java
//SET QUESTION STRING TO TEXTVIEW
textQuestion.setText(questionDatabase.getQuestion(index));
//SET ANSWER STRINGS TO BUTTONS' TEXT
buttonOne.setText(questionDatabase.getAnswer(index, 0));
buttonTwo.setText(questionDatabase.getAnswer(index, 1));
buttonThree.setText(questionDatabase.getAnswer(index, 2));
buttonFour.setText(questionDatabase.getAnswer(index, 3));
“NEXT”和“PREVIOUS”按钮允许用户通过增加/减少“索引”来浏览问题。
因此,因此,活动永远不知道(我不认为)哪些问题/答案需要文本作为上标,以及为什么我希望能够自己设置问题/答案子/上标我将问题/答案输入到数组中。这个可以吗?
编辑:@jared burrows 在聊天中非常有帮助,帮助我几乎解决了问题。我现在可以使用 setText(Html.fromHtml());在我的文本视图(问题文本)上,它正确显示了下标和上标。但是在我的按钮(答案)上,我尝试采用相同的策略,但效果不一样。 “X2”就变成了“X2”。为什么是这样?为什么我的按钮不像 textview 一样接受 html。
【问题讨论】:
标签: java android string subscript superscript