簡體   English   中英

在資產文件夾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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM