java - 如果用户输入某个关键字并显示它,我如何让editText获取stringarray的值?
问题描述
美好的一天,我正在制作一个需要打字才能回答的测验应用程序,而且我是编程新手。我想知道如果他们没有得到确切的答案,如何或是否可以正确回答问题。
因为给定的问题有一个句子形式的答案,例如:
问:“树的三个主要部分是什么?”
A. “三个主要部分是枝、叶、根。”
如果他们只回答“Branch, Leaves, root”,我将使其正确并显示相同的 editText 或 Textview 或某些弹出消息的确切答案。我想我只需要另一个 ArrayList 作为关键字吗?这种东西的代码是什么?顺便说一句,我有超过 300 个 Q/A。
这是我的编辑文本:
answer1.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
mp2.start();
switch (category) {
case "Basic":
if (answer1.getText().toString().equalsIgnoreCase(BasicList.get(level).getbasicans1())) {
Toast.makeText(QuestionActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(QuestionActivity.this, "Wrong!", Toast.LENGTH_SHORT).show();
}
break;
这是我的数组列表。
final ArrayList<BasicItem> BasicList = new ArrayList<>();
BasicList.add(new BasicItem("What is the Figurative meaning of [1]Water, [2]Field, [3]Fire?", "Word as as ds ds", "Example of example only", "Word that consume"));
BasicList.add(new BasicItem("What is the meaning of [1]B,[2]D,[3]S?", "Betrayal", "Destruction", "Salvation");
谢谢您的帮助 :)
解决方案
好的..你可以试试这个
String sentence = "Check this answer and you can find the keyword with this code";
String search = "keyword";
if ( sentence.toLowerCase().indexOf(search.toLowerCase()) != -1 ) {
System.out.println("I found the keyword");
} else {
System.out.println("not found");
}
或者
if ( d.contains("hello")) {
System.out.println("I found the keyword");
}
或者,如果你想使用字符串匹配,
if (d.matches(".*Hey.*")) {
c.setText("OUTPUT: SUCCESS!");
} else {
c.setText("OUTPUT: FAIL!");
}
.* -> 0 个或多个任意字符
嘿 -> 你想要的字符串
推荐阅读
- javascript - Chartjs 显示数字而不是时间
- php - 如何分解数组并插入 MySQL
- scala - 当将 snakeyaml 反序列化对象传递给 Gatling 馈线时,少量会导致 java.lang.ClassCastException
- c++ - 有没有办法在 GCC 中禁用内联汇编程序?
- spring - Spring Jpa 实体 - EntityManager.getReference
- sql - 在 Redshift/PostgreSQL 中使用 Last_Value 窗口函数
- python - 我如何获得一个程序来计算文件中每个员工的加薪?
- openstreetmap - 如何在立交桥中塑造国家(没有海上限制......只有地形)?
- python - 确定列表中大于阈值的项目的最有效方法是什么?
- symfony - 组 Symfony/Monolog 日志消息