格式化文本文件以在android中的EditText中显示

时间:2011-08-04 21:01:21

标签: android android-edittext android-ui

我让我的小html渔夫工作,它从URL字符串中抓取一个文本文件。我可以将setText调用到我的EditText视图,它确实会在文本文件中显示文本,但是根本没有格式化(我说的是简单的东西,如回车和换行)。如何在EditText中更好地渲染它?原始文本文件如下所示:

想象一下资源中的以下内容:

http://www.someaddress.com/thetextfile.txt

Original Text File here.  

1. First thing on the text file.

2. Second thing on the text file...

   Finally the end of the text file.  This is a long string blah blah
that I like to use here.  Notice the carriage return line breaks and indentation
on the paragraph.

我可以将上面的字符串作为字符串,但在EditView中显示时根本没有回车符。无论如何我可以添加这个吗?它不是添加\ n或\ r的问题,因为那些已经存在于我怀疑的文本文件中(用Notepad ++编写)。那么无论如何都要让这一点变得更加格式化了吗? (并在将字符串保存回磁盘或数据库时保留格式化?

编辑:

<EditText android:id="@+id/contract_text_input"
        android:layout_width="650sp" android:layout_height="wrap_content"
        android:lines="25"
        android:scrollbars = "vertical"
         android:gravity="top|left" android:inputType="textMultiLine"
        android:scrollHorizontally="false" 
        android:minWidth="10.0dip"
        android:maxWidth="5.0dip"/>

编辑:

这些是从互联网上获取此文本文件的方法。他们似乎正在剥夺所有\ n和\ r。但是,如果我检查那个在线的文件,它只会在其中。那么也许它的filezilla上传到我的文件的网络服务器?

public static InputStream getInputStreamFromUrl(String url){
            InputStream contentStream = null;

            try{
              HttpClient httpclient = new DefaultHttpClient();
              HttpResponse response = httpclient.execute(new HttpGet(url));
              contentStream = response.getEntity().getContent();
            } catch(Exception e){
               e.printStackTrace();
            }
            return contentStream;
         }

  public static String getStringFromUrl(String url)  {
         BufferedReader br = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));

         StringBuffer sb = new StringBuffer();

         try{
          String line = null;

          while ((line = br.readLine())!=null){
           sb.append(line);
          }
         }catch (IOException e){
          e.printStackTrace();
         }
         return sb.toString();
  }

这就是我最终更新EditText的方式:

private class FragmentHttpHelper extends AsyncTask<Void, Void, String>{
         protected void onPostExecute(String result) {               


                contractTextTxt.setText(result);
            }
        @Override
        protected String doInBackground(Void... params) {

            return getStringFromUrl(urlReferenceTxt.getText().toString());

        }         
    }

2 个答案:

答案 0 :(得分:2)

检查Android API。问题出现在以下代码行中:

while ((line = br.readLine())!=null)
{
    //code goes here
}

对readLine()的调用会删除换行符,回车符和换行符。如果你想获得所有东西,你需要使用read()。

答案 1 :(得分:1)

对不起,我刚刚在我的应用程序中做了类似的事情

并致电

((EditText) findViewById(R.id.textView1))
            .setText("Original Text File here.\r\n\r\n1. First thing on the text file.\r\n\r\n2. Second thing on the text file...\r\n\r\n   Finally the end of the text file.  This is a long string blah blah that I like to use here.  Notice the carriage return line breaks and indentation on the paragraph.");

与你的字符串相同

我得到了

enter image description here

对我来说很好看!

我的编辑文字是

<EditText   android:id="@+id/textView1" android:layout_width="wrap_content"
    android:layout_weight="0" android:layout_height="wrap_content"></EditText>

编辑抱歉巨大的图片

edit2:检查调试器的字符串值 enter image description here