2013. 7. 24. 16:08
public class MainActivity extends Activity implements OnClickListener {
	int mMainValue = 0;
	int mBackValue = 0;
	TextView mMainText;
	TextView mBackText;
	Button increase;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mMainText = (TextView)findViewById(R.id.mainvalue);
		mBackText = (TextView)findViewById(R.id.backvalue);
		increase = (Button)findViewById(R.id.increase);

		increase.setOnClickListener(this);

		BackThread thread = new BackThread();
		thread.setDaemon(true);
		thread.start();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		mMainValue++;
		mMainText.setText("MainValue : " + mMainValue);
	}

	class BackThread extends Thread {
		public void run() {
			while (true) {
				mBackValue++;
				mHandler.sendEmptyMessage(0);
				try { Thread.sleep(1000); } catch (InterruptedException e) {;}
			}
		}
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			if(msg.what ==0) {
				mBackText.setText("BackValue : " + mBackValue);
			}
		}
	};
 

}

'Android > 개발팁' 카테고리의 다른 글

태블릿 분기하기  (0) 2013.10.08
Gradient buttons  (0) 2013.08.22
★ setBackground vs setBackgroundDrawable / AlertDialog  (0) 2013.07.12
[Android] keyboard 감지  (0) 2013.07.08
How to replace R.drawable.“someString”  (0) 2013.06.25
Posted by hoonihoon