我尝试创建一个类似字典的应用程序(使用HashMap
),用户在textEdit
中键入单词(行话),如果该单词与任何字匹配HashMap
中的字词,当用户点击textView
时,会在enter
中显示该字词的预定义含义。
这是我的java code
:
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
{
// read input
EditText searchField = (EditText)findViewById(R.id.searchField);
String word = searchField.getText();
// Error: I'm being prompted to convert this to Editable, should I?
//display output;
String meaning = lookup(word);
TextView displayField = (TextView)findViewById(R.id.displayField);
displayField.setText(meaning);
// I'm getting a multiple syntax error marker at this line
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map<String, String> dictionary = new HashMap<String, String>();
dictionary.put("Doe", "a deer, a female deer");
dictionary.put("Ray", "a drop of golden sun");
}
public String lookup (String input){
return dictionary.get(input);
// Error at this line: dictionary cannot be reslolved, multiple eclipse suggestions none of which seem to work
}
这是我的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.safelintels.hgsdreamdictionary.MainActivity" >
<EditText
android:id="@+id/searchField"
android:textSize="25sp"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter word"
android:textColor="#000000"
android:lines="1"
android:imeOptions="actionSearch"
android:maxLines="1"
tools:ignore="RtlHardcoded,TextFields,HardcodedText,UnusedAttribute" />
<TextView
android:id="@+id/displayField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/searchField"
android:layout_alignLeft="@+id/searchField"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/searchField"
android:layout_below="@+id/searchField"
android:textColor="#000000"
android:textSize="20sp"
tools:ignore="HardcodedText,RtlHardcoded,RtlCompat" />
</RelativeLayout>
在本网站和其他网站上提供的有些类似问题的大部分答案建议创建database
。但是,我不想要这种方法,因为我不会存储这么多的单词,只有几个单词和含义。
提前感谢您提出的善意建议。
答案 0 :(得分:2)
我建议使用Map类而不是嵌套if语句,因为这样可以更快地工作。 Map被索引,这意味着在找到匹配项之前,您的程序将立即转到Map中的匹配条目,而不是评估每个if条件。如果在地图中找不到输入,则会返回null
。
Map<String, String> dictionary = new HashMap<String, String>;
dictionary.put("lunch", "the meal you eat in the afternoon");
dictionary.put("dinner", "the meal you eat after lunch");
public String lookup (String input) {
return dictionary.get(input);
}
如果您使用if语句已经死定,我建议您使用switch
。它的作用与if / else if语句的长列表相同,但它更容易阅读。请注意:在Java 7之前,您不能在switch
块中使用字符串,这意味着您将需要单独的if语句。
public String otherLookup(String input) {
switch (input) {
case "lunch":
return "the meal you eat in the middle of the day";
case "dinner":
return "the meal you eat after lunch";
case "other":
return "all your other strings here; very tedious";
}
return "";
}
在所有情况下,您都会在文本输入中阅读:
//read in user input
EditText inputField = (EditText) findViewById(R.id.yourId);
String inputText = inputField.getText();
//display output; you can put this code inside of an onClick type of method if you want it to be triggered by a button click
String outputText = lookup(inputText);
TextView outputDisplay = (TextView) findViewById(R.id.yourOtherId);
outputDisplay.setText(outputText);
编辑答案: 在UI中添加一个按钮。当用户单击此按钮时,它将触发您的字典查找。现在,您的应用正在尝试在应用启动时运行单词查找,然后用户可以在EditText字段中放置任何内容。所以把它放在layout.xml中你有TextView和EditText:
<Button android:id="@+id/lookupButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Look Up Word"
android:onClick="lookup"/>
然后将此代码作为您的主要Activity。对不起,我想我误解了你使用它的上下文。我相信这应该会更好:
public class MainActivity extends ActionBarActivity {
//assign the UI widgets to variables so you can access them later.
EditText searchField = (EditText) findViewById(R.id.searchField);
TextView displayField = (TextView) findViewById(R.id.displayField);
Map<String, String> dictionary = new HashMap<String, String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dictionary.put("Doe", "a deer, a female deer");
dictionary.put("Ray", "a drop of golden sun");
}
//this lookup() method is what gets called when the user clicks the Button.
//this View parameter refers to the View that the user clicks to trigger the method call; in your case, this is the Button.
public void lookup(View view) {
//use .toString() to cast the input to a String that won't be updated whenever the user
// changes what is in the EditText field; sorry, I overlooked that before
String word = searchField.getText().toString();
//this line looks up the value in your dictionary and sticks it in the TextView for the user to see
displayField.setText(dictionary.get(word));
}
}
答案 1 :(得分:0)
最好将UI变量声明为private
,然后在onCreate
method
内初始化它们以避免NullPointerException
error
:
public class MainActivity extends ActionBarActivity {
Map<String, String> map = new HashMap<String, String>();
private EditText searchField;
private TextView displayField;
String word;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchField = (EditText) findViewById(R.id.searchField);
displayField = (TextView) findViewById(R.id.displayField);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String word = searchField.getText().toString();
displayField.setText(map.get(word);
dictionary.put("Doe", "a deer, a female deer");
dictionary.put("Ray", "a drop of golden sun");
}