MasterofProject

Handler update main line UI commonly used method

label Android DevelopmentHandler
72 people read comment(0) Collection report
Classification:

Handler for us to engage in the development of the program is more familiar with the ape. It is a mechanism for Android to update the main thread of UI, it can send messages, receive messages and process information, to achieve UI updates and other operations. Android design has been really can not be directly updated in the sub thread of the main process UI, so we have to be in the development of strict accordance with the relevant mechanisms to deal with.

/ * *
*
* @description: through the Handler to update the TextView UI text display
* 2015-11-30 9:03:18 am @date
* /

Class MainActivity extends Activity public {

TextView test_tv private;
Handler mHandler new = Handler private ();


@Override
Void onCreate protected (savedInstanceState Bundle) {
Super.onCreate (savedInstanceState);
SetContentView (R.layout.activity_main);
Test_tv = (TextView) findViewById (R.id.test_tv);
Thread new () {
@Override
Void run public () {
{try
Sleep (2000);
//test_tv.setText ("update UI data"); if you write directly, do not have Handler will certainly be reported to the wrong, prompt not in non UI thread directly update
MHandler.post (Runnable new) {
@Override
Void run public () {
Test_tv.setText ("update UI data");
}
});
}
Catch (E InterruptedException) {
E.printStackTrace ();
}
}
}.start ();
}
}

----------------------------------------

/ * *
*
* @description: timer by Handler, to achieve the picture carousel
* of course, it is often used in ViewFlipper or ViewPager to achieve.
* 2015-11-30 9:03:18 am @date
* /
Class MainActivity extends Activity public {
ImageView test_iv private;
Handler mHandler new = Handler private ();
Private int[] imgIds ={R.drawable.test_img_01, R.drawable.test_img_02, R.drawable.test_img_03};
Private int currentIndex; / / the current location of the picture
MyRunable myRunable new = MyRunable private ();


@Override
Void onCreate protected (savedInstanceState Bundle) {
Super.onCreate (savedInstanceState);
SetContentView (R.layout.activity_main);
Test_iv = (ImageView) findViewById (R.id.test_iv);
MHandler.postDelayed (myRunable, 1000);
}


MyRunable implements Runnable class {


@Override
Void run public () {
CurrentIndex++;
CurrentIndex = currentIndex% imgIds.length;
Test_iv.setImageResource (imgIds[currentIndex]);
MHandler.postDelayed (this, 1000);
}

}
}

--------------------------------------------------------------

/ * *
*
* @description: send message and message processing via Handler
* 2015-11-30 10:15:46 am @date
* /
Class MainActivity extends Activity public {
TextView test_tv private;
Handler mHandler new = Handler private () {
Void handleMessage public (MSG Message) {
/ / Test_tv.setText (received message for "+msg.arg1+" - "+msg.arg2");
Test_tv.setText (received message object for "+msg.obj");
};
};


@Override
Void onCreate protected (savedInstanceState Bundle) {
Super.onCreate (savedInstanceState);
SetContentView (R.layout.activity_main);
Test_tv = (TextView) findViewById (R.id.test_tv);
Thread new () {
Void run public () {
{try
Sleep (3000); / / time-consuming operation simulation
MSG Message = Message new ();
/ / Msg.arg1 = 100;
/ / Msg.arg2 = 200;
/ / MHandler.sendMessage (MSG);
Send / / object data
Stu=new Student Student ();
Stu.setName ("Billy");
Stu.setAge (30);
Stu.setLevel (5);
Msg.obj=stu;
MHandler.sendMessage (MSG);
}
Catch (E InterruptedException) {
E.printStackTrace ();
}
};
}.start ();
}


}

Out of the message in ------------Handler -----------------------------------------------------

MHandler.removeCallback (runable); / / incoming runable object to the corresponding

top
Zero
step on
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • visit50031 times
    • Integral:Two thousand three hundred and eleven
    • Grade
    • Rank:9825th name
    • original187
    • Reproduced:37
    • Translation:1
    • Comments:13
    Latest comments