E4111673128D20177A7093A5F8F9A6  

main.xml

 

<LinearLayout xmlns:android="HTTP://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
</LinearLayout>

 

ScrollViewActivity

 

public class ScrollViewActivity extends Activity {

 

private static final String TAG = "ScrollViewActivity";
private Handler h;
private boolean print = false;
private TextView tv;

 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

 

this.h = new Handler() {

 

private SimpleDateFormat df = new SimpleDateFormat(
"yyyy/MM/dd hh/mm/ss");

 

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
insert2Tv("現在時刻:" + this.df.format(new Date()));
// 執行狀態才會送出下一個訊息
if (print) {
this.sendMessageDelayed(this.obtainMessage(), 1000);
}
}
};
}

 

@Override
protected void onResume() {
super.onResume();
this.print = true;
// 每次進入立刻送出訊息
this.h.sendEmptyMessage(0);
}

 

@Override
protected void onPause() {
super.onPause();
this.print = false;
}

 

private void insert2Tv(String msg) {
if (this.tv == null) {
this.tv = (TextView) this.findViewById(R.id.tv);
}
this.tv.setText(this.tv.getText().toString() + "\n" + msg);
Log.d(TAG, msg);
}
}
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()