Solution of Setting the Value Prompt of Control in Android Thread and Reporting Error

  • 2021-07-22 11:21:44
  • OfStack

In this paper, the solution of setting the value prompt of control in Android thread is described. Share it for your reference, as follows:

Setting a control value of 1 in an Android thread is generally 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 Adj. alpha Value, 
 int  image_alpha = 255;
 Handler mHandler = new Handler();
 // Control thread 
 boolean isrung = false;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 isrung = true;
 // Obtain ImageView Object of 
 imageview = (ImageView) this.findViewById(R.id.ImageView01);
 textview = (TextView) this.findViewById(R.id.TextView01);
 // Settings imageview Gets or sets the picture resources of. It can also be repeated xml In the layout, write as follows 
 //android:src="@drawable/logo"
 imageview.setImageResource(R.drawable.logo);
 // Settings imageview Adj. Alpha Value 
 imageview.setAlpha(image_alpha);
 // Open 1 Threads to make Alpha Value diminishing 
 new Thread(new Runnable() {
  public void run()
  {
  while (isrung)
  {
   try
   {
   Thread.sleep(200);
   // Update Alpha Value 
   updateAlpha();
   // If you use the code commented below to directly set the imageview The transparency of, textview The value of will report an error, because the control cannot be set in the thread, and you need to use the 1 A Handler To set the related values 
//   if (image_alpha - 7 >= 0)
//   {
//    image_alpha -= 7;
//   }
//   else
//   {
//    image_alpha = 0;
//    isrung = false;
//   }
//   imageview.setAlpha(image_alpha);
//   textview.setText(" Now alpha The value is: "+Integer.toString(image_alpha));
   }
   catch (InterruptedException e)
   {
   e.printStackTrace();
   }
  }
  }
 }).start();
 // Update after receiving the message imageview View 
 mHandler = new Handler() {
  @Override
  public void handleMessage(Message msg)
  {
  super.handleMessage(msg);
  imageview.setAlpha(image_alpha);
  textview.setText(" Now alpha The 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 requires updates imageview Messages for the view 
 mHandler.sendMessage(mHandler.obtainMessage());
 }
}

More readers interested in Android can check the topic of this site: "Android Thread and Message Mechanism Usage Summary", "activity Operation Skills Summary of Android Programming", "Android Debugging Skills and Common Problem Solutions Summary", "Android Development Introduction and Advanced Tutorial", "Android Multimedia Operation Skills Summary (Audio, Video, Recording, etc.)", "Android Basic Component Usage Summary", "Android View View Skill Summary", "Android Layout layout Skill Summary" and "Android Control Usage Summary"

I hope this article is helpful to everyone's Android programming.


Related articles: