Monitor project development path ~ implement a TextView similar to the command line form

In the development process of Monitor project, we want to implement a window similar to Dos command line on the computer. The function is to submit commands to the server, and then display the results returned by the server in a window in the form of characters, which can be added continuously. At the beginning, my idea after thinking is to use a RecyclerView to implement, just like QQ sending and receiving messages. However, this solution was quickly rejected by the team members. Because it does not conform to the overall style of APP, it can only be used as a text only interface according to the original idea. There is no doubt that TextView is chosen. It has an append() method that can append strings at any time.

try {
       InputStream responseIn = mControlOtherMode.resultTranIn;
       final byte[] buffer = new byte[1024];
       while (true) {
             int length = responseIn.read(buffer);
             System.out.println(length);
             final String resonse = new String(buffer, 0, length, "GBK");
             runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                        mTextView.append(resonse);
                  }
             });

             if (length == -1)
                 break;
        }
       System.out.print("Network disconnection 333");

} catch (UnsupportedEncodingException e) {
     e.printStackTrace();
} catch (IOException e) {
     e.printStackTrace();
}

The above code constantly reads data from the input stream in the while loop and uses the mTextView.append (reconnect) to add to TextView, you can do some processing on the data, such as adding a new line after each returned data. In order to make the effect closer to the command line, you may think of what to do when TextView's content reaches the full screen. In fact, just add the following code to make your TextView slide and expand its display range.

mTextView = (TextView) findViewById(R.id.msg_textView);
mTextView.setMovementMethod(ScrollingMovementMethod.getInstance());

Of course, you can also further optimize your command line. Imagine that after you call the soft keyboard to input the command, if the command line can automatically slide to the message location just returned, it is not very good. Then just calculate the target location to be moved after you call append(), and then call mTextView.scrollTo() method.

runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                        mTextView.append(resonse);
                        //Automatically move to the bottom of the text
                        offset = mTextView.getLineCount() * mTextView.getLineHeight();
                        if (offset > mTextView.getHeight()) {
                               mTextView.scrollTo(0, offset - mTextView.getHeight() + mTextView.getLineHeight());
                        }
                  }
});

The following is the final implementation effect, which looks pretty good.

Tags: network

Posted on Sun, 31 May 2020 08:05:58 -0700 by mattsutton