Android Update UI Method Rollup _android

Source: Internet
Author: User

1, activity of the Runonuithread

TextView = (TextView) Findviewbyid (r.id.tv);
New Thread (New Runnable () {
@Override public
void Run () {
runonuithread (new Runnable () {
@Override Public
void Run () {
textview.settext (update UI);}}}
)
. Start ();

The Android Activity Runonuithread () method uses

2, Handler sendemptymessage ()

Package lib.com.myapplication;
Import Android.os.Handler;
Import Android.os.Message;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Mainactivity extends Appcompatactivity {
private TextView TextView;
Handler Handler = new Handler () {
@Override public
void Handlemessage (msg) {
super.handlemessage ( msg);
Textview.settext ("UI updated");
}
;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView = (TextView) Findviewbyid (r.id.tv);
New Thread (New Runnable () {
@Override public
void Run () {
try {
thread.sleep);
} catch (I Nterruptedexception e) {
e.printstacktrace ();
}
Handler.sendemptymessage (2);
}
}). Start ();
}

3, Handler post ()

Package lib.com.myapplication;
Import Android.os.Bundle;
Import Android.os.Handler;
Import android.support.v7.app.AppCompatActivity;
Import Android.widget.TextView;
public class Mainactivity extends Appcompatactivity {
private TextView TextView;
Handler Handler = new Handler ();
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView = (TextView) Findviewbyid (r.id.tv);
New Thread (New Runnable () {
@Override public
void Run () {
try {
thread.sleep);
} catch (I Nterruptedexception e) {
e.printstacktrace ();
}
Handler.post (New Runnable () {
@Override public
void Run () {
textview.settext ("UI updated");
}
) ;
}
}). Start ();
}

4, view Post ()

TextView = (TextView) Findviewbyid (r.id.tv);
New Thread (New Runnable () {
@Override public
void Run () {
try {
thread.sleep);
} catch (Interr Uptedexception e) {
e.printstacktrace ();
}
Textview.post (New Runnable () {
@Override public
void Run () {
textview.settext ("UI updated");
}
) ;
}
}). Start ();

Summarize:

1, in fact, the above four ways can be attributed to one way: handler for the communication between the Android thread.

2. Why does Android require UI operations only on the UI thread? Mainly in order to avoid multithreading caused by concurrency problems. The single threaded operation UI is safe.

The above is to introduce you to the Android Update UI method Summary of the relevant knowledge, hope to help everyone, if you have questions welcome to my message, small series will promptly reply to everyone!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.