从资产文件夹 ANDROID STUDIO 的文本文件中查找单词
Find words in text file from assets folder ANDROID STUDIO
我的应用程序有问题。
我想要做的是找到用户在文本字段中输入的单词的押韵。
我在资产文件夹中有一本名为 "words.txt" 的字典。
我的应用程序几乎可以正常工作,但它只能从我的文本文件中找到一个词。
我怎样才能找到与文本字段中的单词押韵的所有单词?
有任何想法吗?
这是我的代码:
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
editTextWord = (EditText)findViewById(R.id.editTextWord);
b_read = (Button)findViewById(R.id.buttonSearch);
tv_text = (TextView)findViewById(R.id.nameTxt);
b_clear = (Button)findViewById(R.id.buttonClear);
b_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
}
});
b_read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
InputStream is = getAssets().open("words.txt");
int size = is.available();
BufferedReader rd = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String line;
String word = editTextWord.getText().toString();
if (word.isEmpty()){
Toast.makeText(getApplicationContext(), "Wpisz słowo", Toast.LENGTH_SHORT).show();
return;
}
while ((line = rd.readLine()) !=null ){
if (line.substring(line.length()-2).equals(word.substring(word.length()-2))){
tv_text.setText(line);
}
}
rd.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
});
}
tv_text.setText 覆盖 TextView 中已有的任何内容。你需要使用类似 TextView.append(CharSequence)
尝试:
tv_text.append(line + "\n");
我的应用程序有问题。 我想要做的是找到用户在文本字段中输入的单词的押韵。 我在资产文件夹中有一本名为 "words.txt" 的字典。 我的应用程序几乎可以正常工作,但它只能从我的文本文件中找到一个词。 我怎样才能找到与文本字段中的单词押韵的所有单词? 有任何想法吗? 这是我的代码:
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
editTextWord = (EditText)findViewById(R.id.editTextWord);
b_read = (Button)findViewById(R.id.buttonSearch);
tv_text = (TextView)findViewById(R.id.nameTxt);
b_clear = (Button)findViewById(R.id.buttonClear);
b_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
}
});
b_read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
InputStream is = getAssets().open("words.txt");
int size = is.available();
BufferedReader rd = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String line;
String word = editTextWord.getText().toString();
if (word.isEmpty()){
Toast.makeText(getApplicationContext(), "Wpisz słowo", Toast.LENGTH_SHORT).show();
return;
}
while ((line = rd.readLine()) !=null ){
if (line.substring(line.length()-2).equals(word.substring(word.length()-2))){
tv_text.setText(line);
}
}
rd.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
});
}
tv_text.setText 覆盖 TextView 中已有的任何内容。你需要使用类似 TextView.append(CharSequence)
尝试:
tv_text.append(line + "\n");