新聞中心
怎么在Android項(xiàng)目中實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁面功能?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
創(chuàng)新互聯(lián)建站是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的10余年時(shí)間我們累計(jì)服務(wù)了上千家以及全國政企客戶,如成都紙箱等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實(shí)力獲得客戶的一致贊譽(yù)。
示例代碼
1.layout中新建兩個(gè)xml文件,在src下的包中新建兩個(gè)類,MainActivity和MainActivity2并分別指向兩個(gè)xml文件,在MainActivity的指向的xml文件建一個(gè)TextView控件,用于倒計(jì)時(shí)的顯示。
2.MainActivity文件中的代碼如下
package com.example.demo1; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.textView1); handler.post(waitSendsRunnable); } //啟用一個(gè)Handler Handler handler = new Handler() { @SuppressLint("HandlerLeak") public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: Intent intent = new Intent(MainActivity.this, MainActivity2.class); startActivity(intent); break; case 1: tv1.setText("倒計(jì)時(shí):" + index + "s"); break; default: break; } } }; // 倒計(jì)時(shí)五秒 int index = 5; Runnable waitSendsRunnable = new Runnable() { public void run() { if (index > 0) { index--; try { Thread.sleep(1000); handler.sendEmptyMessage(1); } catch (InterruptedException e) { e.printStackTrace(); } handler.post(waitSendsRunnable); } else { try { Thread.sleep(1000); handler.sendEmptyMessage(0); } catch (InterruptedException e) { e.printStackTrace(); } } } }; }
看完上述內(nèi)容,你們掌握怎么在Android項(xiàng)目中實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁面功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前文章:怎么在Android項(xiàng)目中實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁面功能
文章出自:http://www.ef60e0e.cn/article/jpsesg.html