是否要调用If(inputtext.equals(string.name))我想访问我的string.xml项目名称 [英] Is there a call for If (inputtext.equals(string.name)) i would like to access my string.xml items name

查看:105
本文介绍了是否要调用If(inputtext.equals(string.name))我想访问我的string.xml项目名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串名称中分别有几个文本,想查找是否与输入的字符串名称id相匹配.我将它们放在数组中,如果随机化与输入匹配,则将它们随机取一个.

I have a couple of text in my string names separately and would like to find if matches with input to the strings name id. I have put them in array and randomly taking one if the randomized matches with the input.

<string name="a">あ</string>
<string name="i">い</string>
<string name="u">う</string>
<string name="e">え</string>
<string name="o">お</string>
<string name="ka">か</string>
<string name="ki">き</string>
<string name="ku">く</string>
<string name="ke">け</string>
<string name="ko">こ</string>

<string-array name="kana">
    <item>@string/a</item>
    <item>@string/i</item>
    <item>@string/u</item>
    <item>@string/e</item>
    <item>@string/o</item>
    <item>@string/ka</item>
    <item>@string/ki</item>
    <item>@string/ku</item>
    <item>@string/ke</item>
    <item>@string/ko</item>
</string-array>

有这样的呼吁吗?

if (answer.equals(kanaArray[generatedIndex])) {
                    Toast.makeText(getApplicationContext(), "Sugoi!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "wrong!", Toast.LENGTH_SHORT).show();

                }

这显示了字符串中的实际文本是什么

this showing what is the actual text in the string

屏幕图像

谢谢

对不起.用户将输入像ka,a,ki这样的输入,并且该输入应与字符串id匹配.

Sorry. the user is to input like ka, a, ki and that input is to be matched with the string id.

推荐答案

好,现在我知道您正在使用EditText进行用户输入,而不是使用按钮进行假设.我认为没有一种获取平假名字符串名称字符串的好方法.但是,您可以通过消除假名数组并直接在getIdentifier中使用平假名字符串的字符串名称来有效地执行相同的操作.这就是我的意思:

Ok, now I KNOW you are using an EditText for user input instead of my assumption with buttons. I don't think there's a good way to get the name strings of your hiragana strings. However, you can effectively do the same thing by eliminating the kana array and using the string names of your hiragana strings directly with getIdentifier. This is what I mean:

//I'm making assumptions still, but here I'm somehow getting to the kana
TextView flashcard = (TextView) findViewById(R.id.flashcard);
String kana_char = flashcard.getText();

例如,此处的应用程序包类为"com.example.kanaflashcards"或项目的任何名称,因此resolved_kana成为输入罗马字的假名,如果输入的不是有效罗马字,则为空字符串细绳.然后,您可以将其与显示的kana_char相匹配.

Here the application package class is, for example, "com.example.kanaflashcards" or whatever your project is named, and so resolved_kana becomes the kana of which ever romanji was entered, or empty string if what was entered was not a valid romanji string. You can then match that against the kana_char you displayed.

String user_input = "<value from edit text>";
String resolved_kana = (String) getResources().getText(getResources().getIdentifier(user_input, "string", "<application package class>"),"");
if (resolved_kana.equals(kana_char) {
    Toast.makeText(getApplicationContext(), "Sugoi!", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(getApplicationContext(), "wrong!", Toast.LENGTH_SHORT).show();
}

这篇关于是否要调用If(inputtext.equals(string.name))我想访问我的string.xml项目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆