Android应用开发的第1天。所以,请原谅我(也许)愚蠢的问题:如何将我的申请stdout/stderr
提交给TextView
?
答案 0 :(得分:2)
我想您希望将发送到 System.out 的输出重定向到 System.out.println(),以便在EditText中显示。为此:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_stdout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="false" />
</RelativeLayout>
//Set your layout with setContentView() or inflate it if in fragment
final EditText editText = (EditText) findViewById(R.id.et_stdout);
System.setOut(new PrintStream(new OutputStream() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@Override public void write(int oneByte) throws IOException {
outputStream.write(oneByte);
editText.setText(new String(outputStream.toByteArray()));
}
}));
//Testing the System.out stream
System.out.println("Test");
System.out.println("Test 2");
这会将使用 System.out.println()创建的所有输出写入EditText。