Why am i not able to print escape sequence in edit

2019-07-04 03:08发布

I am getting a response from an API using HTTP POST request which contains which contains some escape sequences such as \n, \t etc. When i try to display it in editText it is displayed as \n and \t but i expect a new line or horizontal tab. This is my code :

String out1 = sys.getString("stdout");
Toast.makeText(getApplicationContext(), out1, Toast.LENGTH_LONG).show();
String time1 = sys.getString("time");
if(compile.length()>0 && time1.equalsIgnoreCase("null")){
    et.setText("Compilation error  "+"\n"+compile);
}
else if(compile.length()>0){
    et.setText("Runtime error  "+"\n"+compile);
}
else
    et.setText(message.substring(2,message.length()-2)+"  time: "+time1.substring(1,Math.min(time1.length(),4))+" memory: "+mem.substring(1,mem.length()-1)+"\n"+out1.substring(2,out1.length()-2));
//et.setText(result);
} catch(Exception e){ 
    e.printStackTrace();
}

The JSON object stdout is something like "stdout":["5\n6"] and on printing it in editText it displays 5\n6 but i expect a new line. Is there any way to handle all escape sequences so as to properly display the result. I have been trying this for quite a while but cannot find a solution. Please guide me. Thanks in advance !!

2条回答
Lonely孤独者°
2楼-- · 2019-07-04 03:45

First, set your editText in xml file like this:

<EditText
        android:id="@+id/editText"
        .
        .
        .
        .
        android:inputType="textMultiLine" >

now put your text with new line character -

et.setText("Compilation error \n compile");

This works, enjoy!!!

查看更多
叛逆
3楼-- · 2019-07-04 03:54

Replace out1.substring(2,out1.length()-2) with out1.

查看更多
登录 后发表回答