zoukankan      html  css  js  c++  java
  • 在Android线程中设置控件的值会报错

    在Android线程中设置控件的值一般会与Handler联合使用,如下:

    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
    {
     //声明ImageView对象
     ImageView imageview;
     TextView textview;
     //ImageView的alpha值,
     int   image_alpha = 255;

     Handler  mHandler = new Handler();
     //控件线程
     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;
      
      //获得ImageView的对象
      imageview = (ImageView) this.findViewById(R.id.ImageView01);
      textview = (TextView) this.findViewById(R.id.TextView01);
      
      //设置imageview的图片资源。同样可以再xml布局中像下面这样写
      //android:src="@drawable/logo"
      imageview.setImageResource(R.drawable.logo);
      
      //设置imageview的Alpha值
      imageview.setAlpha(image_alpha);
      //开启一个线程来让Alpha值递减
      new Thread(new Runnable() {
       public void run()
       {
        while (isrung)
        {
         try
         {

          Thread.sleep(200);
          
          //更新Alpha值
          updateAlpha();
          //如果使用下面注释的代码来直接设置imageview的透明度、textview的值会报错,因为线程中不能对控件进行设置操作,需要使用一个Handler来进行对相关值的设置
    //      if (image_alpha - 7 >= 0)
    //      {
    //       image_alpha -= 7;
    //      }
    //      else
    //      {
    //       image_alpha = 0;
    //       isrung = false;
    //      }
    //      imageview.setAlpha(image_alpha);
    //      textview.setText("现在alpha值是:"+Integer.toString(image_alpha));
          
         }
         catch (InterruptedException e)
         {
          e.printStackTrace();
         }
        }

       }
      }).start();

      //接受消息之后更新imageview视图
      mHandler = new Handler() {
       @Override
       public void handleMessage(Message msg)
       {
        super.handleMessage(msg);
        imageview.setAlpha(image_alpha);
        textview.setText("现在alpha值是:"+Integer.toString(image_alpha));
        //更新
        imageview.invalidate();
       }
      };
     }
     
     public void updateAlpha()
     {
      if (image_alpha - 7 >= 0)
      {
       image_alpha -= 7;
      }
      else
      {
       image_alpha = 0;
       isrung = false;
      }
      //发送需要更新imageview视图的消息
      mHandler.sendMessage(mHandler.obtainMessage());
     }
    }

  • 相关阅读:
    C++ 遇到的问题小结
    学习笔记之 初试Caffe,Matlab接口提取feature
    学习笔记之 初试Linux遇到的问题 2015-10-13
    函数式编程
    如何获取select中的value、text、index相关值 && 如何获取单选框中radio值 && 触发事件 && radio 默认选中
    sessionStorage、localStorage技术相关以及商家sid、sbid记录相关、vue相关问题
    二进制数的妙用
    vue中的坑 --- 锚点与查询字符串
    vue-webpack项目中调试的问题
    JSON中的坑
  • 原文地址:https://www.cnblogs.com/tianguook/p/2422460.html
Copyright © 2011-2022 走看看