Handler類簡介
在Android平臺中,新啟動的線程是無法訪問Activity里的Widget的,當(dāng)然也不能將運行狀態(tài)外送出來,這就需要有Handler機(jī)制進(jìn)行信息的傳遞了,Handler類位于android.os包下,主要的功能是完成Activity的Widget與應(yīng)用程序中線程之間的交互。
開發(fā)帶有Handler類的程序步驟如下:
1. 在Activity或Activity的Widget中開發(fā)Handler類的對象,并重寫handlerMessage方法。
2. 在新啟動的線程中調(diào)用sendEmptyMessage或者sendMessage方法并向Handler發(fā)送消息。
3. Handler類的對象用handlerMessage方法接受消息,然后根據(jù)消息的不同執(zhí)行不同的操作。
Handler使用案例(簡單相冊查看器)
編寫布局文件。activity_handler_sample.xml的代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <ImageView android:id="@+id/myImageView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"/> </LinearLayout>
本文標(biāo)題:Handler制作簡單相冊查看器的實例代碼-創(chuàng)新互聯(lián)
URL地址:http://jinyejixie.com/article34/cshhpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、商城網(wǎng)站、虛擬主機(jī)、ChatGPT、外貿(mào)建站、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容