我對Android編程有點新,我想簡單地設置一些文本到屏幕上。這種功能應該是用於打開和關閉屏幕的功能。打開它時,會打開一個時間戳,並在屏幕上打印一個1。此外,當您關閉它時,會關閉它的時間戳,並在屏幕上打印一個0。我遇到了一些麻煩,只是「追加」到前一個時間戳,這意味着我想要在Android屏幕關閉和屏幕打開時持續記錄。它一直覆蓋自己。這是我的嘗試:Android的setText()覆蓋自己?
protected void onResume(){ //this is for when the screen is turned back on
Time now = new Time();
if(!ScreenReceiver.screenOn){
now.setToNow();
String lsNow = now.format("%m-%d-%Y %I:%M:%S");
LinearLayout lView = new LinearLayout(this);
myText = new TextView(this);
myText.setText(lsNow + ", 1");
lView.addView(myText);
setContentView(lView);
... //more code here
}
protected void onPause(){
Time now = new Time();
if(ScreenReceiver.screenOn){
now.setToNow();
String lsNow = now.format("%m-%d-%Y %I:%M:%S");
LinearLayout lView = new LinearLayout(this);
myText = new TextView(this);
myText.setText(lsNow + ", 0");
lView.addView(myText);
setContentView(lView);
...//more code here
}
如果有人知道解決方案,那太棒了!謝謝!
啊,這太好了!謝謝!只是一個簡單的問題 - TextView text =(TextView)findViewById(R.id.myText)是什麼意思?在這種情況下該變量未被使用。你的意思是text.append(sNow +(screen?「0」「」1「));? – user200081 2013-02-23 21:37:47
@ user200081我注意到了這一點,我正在重寫該方法並在myText中省略」my「。是的,任何一種方式都可以,只要它是相同的變量,並且在佈局中使用'myText'的'TextView'。 – 2013-02-23 21:40:02