如何比较用户在editText中输入的内容与我在项目中内置的数组元素

时间:2015-06-27 08:57:34

标签: android arrays

我有一个字符串数组,在数组下面我有一个toast,它从数组中随机选择元素并将其显示在屏幕上。

在显示吐司的屏幕中,用户输入他在editText中看到的吐司内容。

我的问题:如何将editText中的字符串与从字符串数组中选择的随机元素进行比较?

1 个答案:

答案 0 :(得分:0)

您将在显示之前存储数组中显示的Toast字符串,然后使用if语句比较两者:

//Where you declare your variables and objects @ the top of your class
String toastString = null;

在显示吐司的方法

toastString =  your randomly selected string;
Toast.makeText(getApplicationContext(), toastString, Toast.LENGTH_LONG).show();

在比较方法中

String editTextEntry = editText.getText().toString();
if(editTextEntry.equals(toastString)){
    //do something, display something, whatever you need to do
}
else{
    //Strings didn't match
}