我試圖做一個簡單的詞典,它搜索txt文件中的單詞。我創建了txt文件並存儲了一些英文單詞。我想寫這個詞給EditText。然後,這個單詞將被找到並顯示在TextView上。這是我的代碼。它顯示整個txt文件。我怎樣才能找到具體的詞?我如何從android txt文件中讀取特定文本
public class MainActivity extends Activity {
//creating variables
TextView myText;
EditText myEdit;
Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing the TextView EditText and Button
myText=(TextView) findViewById(R.id.idTextView);
myEdit=(EditText) findViewById(R.id.idEditText);
myButton=(Button) findViewById(R.id.idButton);
myButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String saveData=myEdit.getText().toString();
try{
myText.setText(readfile());
}catch(IOException e){
e.printStackTrace();
}
}
});
}
private String readfile()throws IOException {
String str="";
InputStream is = getResources().openRawResource(R.raw.translate);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line=reader.readLine();
while (line!=null)
{
str = str + line + "\n";
line = reader.readLine();
}
reader.close();
return str;
}
}