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>
ScrollViewActivitypublic 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); } }
關於 Handler 的使用請參考 Android Handler 筆記。
感謝你的這篇文章 對我受用無窮 也學到了 感謝你
回覆刪除