Android的读线的文本文件 [英] Android read line text file

查看:114
本文介绍了Android的读线的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含土耳其所有五个字母的单词的文本文件。从那里,我想画一个随机单词。在所有的单词AssetManager丝网印刷,但不知何故,我没降一个字。如何使用一个循环?

我会很高兴,如果你可以提供帮助。

来源:

 公共类MainActivity延伸活动{    按钮degistir;
    TextView的kelime;
    的EditText ykelime;
    随机RND =新的随机();    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        degistir =(按钮)findViewById(R.id.btngiris);
        kelime =(的TextView)findViewById(R.id.kelime);
        ykelime =(EditText上)findViewById(R.id.giris);        AssetManager assetManager = getAssets();        //加载文本文件
        输入的InputStream;
        尝试{
            输入= assetManager.open(5HarfliKelimeler.txt);            INT大小= input.available();
            字节[]缓冲区=新的字节[大小]
            input.read(缓冲液);
            input.close();            //字节的缓冲区转换为字符串
            字符串文本=新的String(缓冲);            INT卅一= rnd.nextInt(text.length());            kelime.setText(文本);
        }
        赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }        degistir.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根
                kelime.setText(ykelime.getText());
                ykelime.setText(NULL);
            }
        });
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }
}


解决方案

逐行读取文件中的行,并得到所有单词的列表。(的如何逐行读取|查看这里

然后,指定每个按钮的点击随机指数。最后,显示从生成的索引列表中的单词。 (索引范围必须是从0到列表的大小

- 我编辑你的code没有任何编辑器。 -

 公共类MainActivity延伸活动{            按钮degistir;             TextView的kelime;            的EditText ykelime;
            清单<串GT;单词表= NULL;            @覆盖
            保护无效的onCreate(捆绑savedInstanceState){
                super.onCreate(savedInstanceState);
                的setContentView(R.layout.activity_main);                degistir =(按钮)findViewById(R.id.btngiris);
                kelime =(的TextView)findViewById(R.id.kelime);
                ykelime =(EditText上)findViewById(R.id.giris);                AssetManager assetManager = getAssets();                //加载文本文件
                输入的InputStream;
                尝试{
                    输入= assetManager.open(5HarfliKelimeler.txt);
                    单词表=新的ArrayList<串GT;();       读者的BufferedReader =新的BufferedReader(新的InputStreamReader(输入,charset.forName(UTF-8)));                    串线=;                        而((行= reader.readLine())!= NULL){
                           wordList.add(线);
                        }                }
                赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
                degistir.setOnClickListener(新OnClickListener(){                    @覆盖
                    公共无效的onClick(查看为arg0){
                        // TODO自动生成方法存根
                        如果(单词表!= NULL){
                         随机RND =新的随机();
                         整数generatedIndex = rnd.nextInt(wordList.size());
                         字符串selectedWord = wordList.get(generatedIndex);
                         kelime.setText(selectedWord);
                         // ykelime.setText(NULL);
                        }
                          其他{
                          //显示味精ETC.
                         }
                    }                });
            }
            @覆盖
            公共布尔onCreateOptionsMenu(菜单菜单){
                。getMenuInflater()膨胀(R.menu.main,菜单);                返回true;
            }        }

I have a text file which contains all five-letter words in Turkish. From there, I want to draw a random word. AssetManager screen printing on all of the word, but somehow I did not drop a single word. How can I use a loop?

I would be glad if you can help.

Source:

public class MainActivity extends Activity {

    Button degistir;
    TextView kelime;
    EditText ykelime;
    Random rnd =new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        degistir=(Button)findViewById(R.id.btngiris);
        kelime=(TextView)findViewById(R.id.kelime);
        ykelime=(EditText)findViewById(R.id.giris);

        AssetManager assetManager = getAssets();

        // To load text file
        InputStream input;
        try {
            input = assetManager.open("5HarfliKelimeler.txt");

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();

            // byte buffer into a string
            String text = new String(buffer);

            int sayi=rnd.nextInt(text.length());

            kelime.setText(text);
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        degistir.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                kelime.setText(ykelime.getText());
                ykelime.setText(null);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

解决方案

Read file line by line and get all words to a list.(How to read line by line | check here)

Then, specify a random index for each button click. Finally, display the word from the list with generated index. (Index range must be from 0 to size of your list)

-- I edited your code without any editor. --

    public class MainActivity extends Activity {

            Button degistir;

             TextView kelime;

            EditText ykelime;
            List<String> wordList=null;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                degistir=(Button)findViewById(R.id.btngiris);
                kelime=(TextView)findViewById(R.id.kelime);
                ykelime=(EditText)findViewById(R.id.giris);

                AssetManager assetManager = getAssets();

                // To load text file
                InputStream input;
                try {
                    input = assetManager.open("5HarfliKelimeler.txt");
                    wordList= new ArrayList<String>();

       BufferedReader reader = new BufferedReader(new InputStreamReader(input , charset.forName("UTF-8")));

                    String line = "";



                        while ((line = reader.readLine()) != null) {
                           wordList.add(line);
                        }





                } 
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                degistir.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        if(wordList!=null){
                         Random rnd =new Random();
                         Integer generatedIndex = rnd.nextInt(wordList.size());                    
                         String selectedWord=wordList.get(generatedIndex);
                         kelime.setText(selectedWord);
                         // ykelime.setText(null);
                        }
                          else{
                          //DISPLAY MSG ETC.
                         }
                    }

                });
            }
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);

                return true;
            }

        }

这篇关于Android的读线的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆