Set the value of the control in the Android thread the solution for the error _android

Source: Internet
Author: User

The example in this article describes the solution that sets the value of the control in the Android thread to indicate an error. Share to everyone for your reference, specific as follows:

Setting the value of a control in an Android thread is typically used in conjunction with handler, as follows:

Package com.yarin.android.Examples_04_15;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.widget.ImageView;
Import Android.widget.TextView;
 public class Activity01 extends activity {//Declaration ImageView Object ImageView ImageView;
 TextView TextView;
 ImageView alpha value, int image_alpha = 255;
 Handler Mhandler = new Handler ();
 Control Thread Boolean Isrung = false; /** called the activity is a.
 * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main);
 Isrung = true;
 The object of obtaining ImageView ImageView = (imageview) This.findviewbyid (R.ID.IMAGEVIEW01);
 TextView = (TextView) This.findviewbyid (R.ID.TEXTVIEW01); Sets the picture resource for the ImageView.
 You can also write//android:src= "@drawable/logo" Imageview.setimageresource (R.drawable.logo) in the XML layout as follows;
 Sets the alpha value of the ImageView Imageview.setalpha (Image_alpha); Open a thread to decrement the alpha value new Thread (new Runnable () {public void Run () {while (Isrung) {try {thread.sleep (200);
   Update alpha value Updatealpha (); If you use the following annotated code to directly set the transparency of ImageView, the TextView value will be an error, because the control cannot be set to operate in a thread, and a handler is required to set//if (image_alpha-7 >= 0 //{//Image_alpha = 7;//}//Else//{//Image_alpha = 0;//Isrung = false;//}//Imagevie
W.setalpha (Image_alpha);
   Textview.settext ("Now the alpha value is:" +integer.tostring (Image_alpha));
   catch (Interruptedexception e) {e.printstacktrace ();
 }}}). Start (); Update ImageView View after receiving messages Mhandler = new Handler () {@Override public void Handlemessage (msg) {Super.handlem
  Essage (msg);
  Imageview.setalpha (Image_alpha);
  Textview.settext ("Now the alpha value is:" +integer.tostring (Image_alpha));
  Update imageview.invalidate ();
 }
 };
 public void Updatealpha () {if (image_alpha-7 >= 0) {image_alpha = 7;
  else {image_alpha = 0;
 Isrung = false; //Send a message that needs to update the ImageView view Mhandler.sendmessage (mhandleR.obtainmessage ());

 }
}

For more information on Android-related content readers can view the site: "The Android thread and message mechanism usage Summary", "Android programming activity Operation Skills Summary", "Android debugging techniques and common problems solution summary", " Android Development Primer and Advanced tutorials, Android Multimedia how-to summary (audio, video, recording, etc), summary of Android Basic components usage, Android View view tips, Android layout layout tips and A summary of the usage of Android controls

I hope this article will help you with the Android program.

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.