package com.naver.tansanc.test01;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test01);
textView = (TextView) findViewById(R.id.textview);
}
public void mOnClick(View v) {
if (v.getId() == R.id.button) {
Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
countThread ct = new countThread(mHandler);
ct.start();
} else if (v.getId() == R.id.button2) {
Toast.makeText(this, "Hello2", Toast.LENGTH_SHORT).show();
}
}
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 3:
textView.setText((String) msg.obj);
}
}
};
}
class countThread extends Thread {
public void run() {
super.run();
int i = 0;
while (true) {
Message msg = new Message();
msg.what = 3;
msg.obj = (Object) ("Hello " + i);
mHandler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
}
}
Handler mHandler;
countThread(Handler mHandler) {
this.mHandler = mHandler;
}
}
Test01.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" >
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mOnClick"
android:text="Hello"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mOnClick"
android:text="Hello2"/>
</LinearLayout>
'Programming > Android' 카테고리의 다른 글
Android google Map v2 (2) | 2013.04.27 |
---|---|
안드로이드 APK 추출하기 (0) | 2013.04.25 |
테이블 동적 생성 (0) | 2013.03.23 |
TabActivity 사용법 (0) | 2013.03.23 |
다중 액티비티 예제 (0) | 2013.03.23 |