如何显示TextView的西班牙文本? [英] How to show Spanish Text in TextView?

查看:82
本文介绍了如何显示TextView的西班牙文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code和我试图表明,在西班牙文本视图哪些文本。当我运行应用程序,然后它显示?在一些地方。谁能告诉我详细的程序显示西班牙语。

 保护无效的onCreate(捆绑savedInstanceState){
   super.onCreate(savedInstanceState);
   的setContentView(R.layout.information);
   TextView的=(的TextView)findViewById(R.id.information);
       textview.setText(readTxt());
}私人字符串readTxt(){
    为InputStream的InputStream = getResources()openRawResource(R.raw.info)。
    ByteArrayOutputStream byteArrayOutputStream =新ByteArrayOutputStream();
    INT I;
    尝试{
    I = inputStream.read();
        而(ⅰ!= -1)
        {
            byteArrayOutputStream.write(ⅰ);
            I = inputStream.read();
        }
        inputStream.close();
    }赶上(IOException异常五){
            e.printStackTrace();
    }
        返回byteArrayOutputStream.toString();
}


解决方案

您readTxt方法是错误的。
您正在返回你的 ByteArrayOutputStream 而不是实际的一串一串的再presentation。

尝试读取输入流成 ByteArrayInputStream的,然后从中获取字节数组,并在返回新的字符串(字节阵列);

 私人字符串readTxt(){
为InputStream的InputStream = getResources()openRawResource(R.raw.info)。
        InputStreamReader的isReader =新的InputStreamReader(InputStream的);        读者的BufferedReader =新的BufferedReader(isReader);        StringBuffer的缓冲区=新的StringBuffer();
        串线= NULL;
        而((行= reader.readLine())!= NULL)
        {
            buffer.append(线);
            buffer.append(\\ n);
        }
        buffer.deleteCharAt(buffer.length() - 1); //删除最后一个新行字符
        // TODO:不要忘记关闭所有流和读者
        返回buffer.toString();
    }

I have the following code and i try to show text in text-view which in Spanish. When I run app then it showing ? at some places. Can anyone tell me detailed procedure for showing Spanish.

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.information);
   textview=(TextView) findViewById(R.id.information);
       textview.setText(readTxt());
}

private String readTxt(){
    InputStream inputStream = getResources().openRawResource(R.raw.info);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i;
    try {
    i = inputStream.read();
        while (i != -1)
        {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
            e.printStackTrace();
    }
        return byteArrayOutputStream.toString();
} 

解决方案

Your readTxt method is wrong. You are returning a String representation of your ByteArrayOutputStream and not the actual String.

Try reading the input stream into a ByteArrayInputStream and then getting the byte array from it and on that return new String(byteArray);

    private String readTxt(){               
InputStream inputStream = getResources().openRawResource(R.raw.info);
        InputStreamReader isReader = new InputStreamReader(inputStream);

        BufferedReader reader = new BufferedReader(isReader);

        StringBuffer buffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null)
        {
            buffer.append(line);
            buffer.append("\n");
        }
        buffer.deleteCharAt(buffer.length() - 1); // Delete the last new line char
        // TODO: Don't forget to close all streams and readers
        return buffer.toString();   
    }

这篇关于如何显示TextView的西班牙文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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