안드로이드(스튜디오)/막 써
단순 텍스트 로그용 액티비티
어비서
2017. 11. 27. 20:55
반응형
Log_Activity.java
package com.abyser.activity;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import com.abyser.R;
import java.text.SimpleDateFormat;
/**
* <pre>
* 텍스트 결과 출력 액티비티.
* </pre>
* Created by abyser on 2017-11-23.
*/
public class Log_Activity extends Activity {
private static final String LOG_TAG = "Log_Activity";
private ScrollView sv_log;
private TextView tv_log;
private Button btn_log;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log);
initLayout();
}
private void initLayout() {
sv_log = (ScrollView) findViewById(R.id.log_sv_log);
tv_log = (TextView) findViewById(R.id.log_tv_log);
btn_log = (Button) findViewById(R.id.log_btn);
btn_log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// TODO 이벤트
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void setLog(String msg) {
String origin_msg = "";
if (tv_log.getText().toString() != null && tv_log.getText().toString().length() > 0) {
origin_msg = tv_log.getText().toString();
}
String time = new SimpleDateFormat("HH:mm:ss").format(System.currentTimeMillis());
if (msg == null || msg.length() <= 0) {
tv_log.setText(origin_msg + "\n" + time + " : empty");
} else {
tv_log.setText(origin_msg + "\n" + time + " : " + msg);
}
sv_log.fullScroll(ScrollView.FOCUS_DOWN);
}
}
activity_log.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/log_sv_log"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/log_tv_log"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="this is log textview"
android:textSize="15dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/log_btn"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
반응형