weixin_43698148 2019-05-25 23:56 采纳率: 0%
浏览 660

android中为什么\n换不了行

package com.example.myapplication;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class BookContent extends AppCompatActivity {
    private TextView tv_con;
    private String textPath = "";//txt文件网址
    //private String content = new String(); //txt文件内容
    private static final int CHANGE_CONTENT = 1;
    private static final int ERROR = 2;




    //主线程消息处理
    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            if (msg.what == CHANGE_CONTENT) {
                String content = (String)msg.obj;
                tv_con.setText(content);
            }else if(msg.what == ERROR){
                Toast.makeText(BookContent.this,"显示文本内容错误!",Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_content);
        Intent intent = getIntent();
        textPath = intent.getStringExtra("textPath");
        tv_con = (TextView) findViewById(R.id.tv_con);
        getContentInfo();
        tv_con.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    public void getContentInfo(){
        //在子线程中获取服务器的数据
        Thread thread = new Thread(){
            HttpURLConnection conn;
            @Override
            public void run() {
                //1:确定地址
                try {
                    URL url = new URL(textPath);
                    //建立连接
                    conn = (HttpURLConnection) url.openConnection();
                    //设置请求方式
                    conn.setRequestMethod("GET");
                    //设置请求超时时间
                    conn.setConnectTimeout(500);
                    //设置读取超时时间
                    conn.setReadTimeout(500);
                    //判断是否获取成功
                    if(conn.getResponseCode() == 200) {
                        //获得输入流
                        InputStreamReader isReader = new InputStreamReader(conn.getInputStream(),"utf-8");
                        BufferedReader buffer = new BufferedReader(isReader);
                        String line = null;
                        StringBuffer sb = new StringBuffer();
                           while ((line = buffer.readLine()) !=null){
                               sb.append(line + "\n");
                           }
                        //将更改界面的消息发给主线程
                        Message msg = new Message();
                        msg.what = CHANGE_CONTENT;
                        msg.obj = sb.toString();//将文本信息传给obj
                        handler.sendMessage(msg);
                    } else{
                        Message msg = new Message();
                        msg.what = ERROR;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = ERROR;
                    handler.sendMessage(msg);
                }
                //关闭连接
                conn.disconnect();
            }
        };

        //启动线程
        thread.start();
    }


}
  • 写回答

1条回答 默认 最新

  • 代码的灵魂是bug! 2019-05-26 13:34
    关注

    试一试这个行不行\r\n

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿