Android edittext丢失并重复通过TCP套接字接收的文本

时间:2019-09-10 10:39:22

标签: android sockets tcp android-edittext

我在Android Studio中的编辑文本有问题。

通常,log.d报告通过TCP套接字接收的消息,并且 这些出现在textedit mySocketrx中。有斋戒的时候 消息爆发log.d仍然可以正常工作,但是textedit丢失并重复 消息。

protected void onCreate(Bundle savedInstanceState) {

    final EditText mySocketrx = (EditText) findViewById(R.id.Socketrx);

    new Thread(new Runnable() { // Client TCP socket thread
                public void run() {
                        try {
                            s = new Socket("192.168.1.161",6000);
                            p = new PrintStream(s.getOutputStream());
                            b = new BufferedReader ( new InputStreamReader( s.getInputStream() ) );
                        } catch(Exception ex) { }
            while (true) {
                            try {
                                    status = b.readLine();
                                    Log.d("DEBUG", status);
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                                mySocketrx.append(status + "\n");
                                        }
                                    } );
                            } catch(Exception ex) { }
                        }
        }
        }).start();
}

1 个答案:

答案 0 :(得分:0)

使用setText()方法代替append()。