翻译从文本文件中逐行读取并与editText

时间:2016-01-15 09:52:46

标签: android file-io android-edittext

我对App Development非常感兴趣。但是我只在两个月前开始,所以我没有经验......我试着为我自己制作一个小翻译应用程序。我有一个用于输入的EditText和用于输出的TextView。想法是从EditText读取,在文本文档中逐行搜索行号 例如:

  1. Apple - Apfel
  2. 车 - 自动
  3. 然后找到单词并将翻译后的单词带到textView 例如:

    EditText字是:Apple 发现这个词Textview是:Apfel

    如果单词未成立,则生成TextView错误消息。

    我有一个想法,为空EditText制作警报对话框 “eingabe”是我在EditText中的文本值 所以我想做的是,Method查看EditText并从那里获取文本。当Field为空时,它会以AlertDialog的形式出现错误消息。如果该字段不为空,则Method将获取Value并将其与我的Assets文件夹中保存的Dictionary进行比较。这就是为什么需要一个InputStream和一个Reader!但我不知道我是如何为代码编写正确的语法。

    这是我的完整方法 eingabe意味着EditText

    public void sucheErgebnis() throws IOException{
    
    
        final Context context = DeutschActivity.this;
        final String str;
        final String eingabe = txtEingabe.getText().toString();
        StringBuffer buf = new StringBuffer();
        InputStream is = this.getResources().getAssets().open("dictionary.rtf");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    
        btnSuch = (Button)findViewById(R.id.btnSuch);
        btnSuch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                if(Objects.equals(eingabe, "")){
    
                    new AlertDialog.Builder(context)
                            .setTitle("Dikkat")
                            .setMessage("Arama alani bos kalamaz!")
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
    
                                    txtEingabe.setSelection(0);
    
                                }
                            })
                            .setIcon(android.R.drawable.ic_dialog_alert).show();
    
                }
                else{
    
    
    
                }
    
    
    
            }
        });
    
    }
    

1 个答案:

答案 0 :(得分:0)

我已编写此代码,该代码将读取存储在asset文件夹中的文件(以您提及的格式)并返回翻译后的单词:

public class MainActivity extends AppCompatActivity {
EditText txtEingabe;
Button  btnSuch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtEingabe = (EditText)findViewById(R.id.txtEingabe);
    btnSuch = (Button)findViewById(R.id.btnSuch);
    try {
        sucheErgebnis();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public void sucheErgebnis() throws IOException {


    final Context context = MainActivity.this;
    final String str;
    StringBuffer buf = new StringBuffer();
    btnSuch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String eingabe = txtEingabe.getText().toString().trim();
            if(eingabe.equals("")){

                new AlertDialog.Builder(context)
                        .setTitle("Dikkat")
                        .setMessage("Arama alani bos kalamaz!")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                txtEingabe.setSelection(0);

                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert).show();

            }
            else{
                String translatedWord = FileReaderFromAssets.read(context, eingabe);
                if(!translatedWord.equals("")){
                   txtEingabe.setText(translatedWord);
                 }
                 else{
                    // write your code here if that word not present in the rtf file
                 }

            }



            }

    });

}

这是搜索文件并返回匹配单词的方法,""如果未找到匹配:

public class FileReaderFromAssets {

    public static String read(Context context, String word){
       String translatedWord = "";
       BufferedReader reader = null;
        try {
            String[] line;
            reader = new BufferedReader(
                new InputStreamReader(context.getAssets().open("dictionary.rtf"))); 

        // do reading, usually loop until end of file reading
        String mLine;
        while ((mLine = reader.readLine()) != null) {
            line = mLine.split("-");
            String temp= line[0].trim();
            if(temp.equalsIgnoreCase(word.trim())){
                translatedWord = line[1].trim();
                break;
            }
        }
        return translatedWord;

    } catch (Exception e) {

        return translatedWord;
    }

    finally{

        try{

            reader.close();

        }catch(IOException ex){

        }

    }

  }
}
相关问题