• 安卓app_sl4_9显示在标题上的进度条功能:创建异步任务


    安卓app_sl4_9显示在标题上的进度条

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearlayout1"
        android:orientation="vertical"
        
        tools:context="com.example.sl4_9.MainActivity" >
    
       
    
    </LinearLayout>
    package com.example.sl4_9;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.LinearLayout.LayoutParams;
    
    public class MainActivity extends Activity {
         private int imageId[] = new int[]{R.drawable.img01,
                 R.drawable.img02,
                 R.drawable.img03,
                 R.drawable.img04};
         private LinearLayout layout1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            layout1=(LinearLayout) findViewById(R.id.linearlayout1);
            new MyTack().execute();
        }
        //功能:创建异步任务,添加4张图片
        class MyTack extends AsyncTask<Void,Integer,LinearLayout>
        {
           @Override
           protected void onPreExecute()
           {
               setProgressBarVisibility(true);
               super.onPreExecute();
           }
            @Override
            protected LinearLayout doInBackground(Void... params) 
            {
                // TODO Auto-generated method stub
                LinearLayout layout2=new LinearLayout(MainActivity.this);
                for(int i=1;i<5;i++)
                {
                    ImageView iv=new ImageView(MainActivity.this);
                    iv.setLayoutParams(new LayoutParams(245,108) );
                    iv.setImageResource(imageId[i-1]);
                    layout2.addView(iv);
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    publishProgress(i);
                    
                }
                return layout2;
            }
            /*
             * 功能:更新进度
             */
            @Override
            protected void onProgressUpdate(Integer...values)
            {
                setProgress(values[0] * 2500);
                super.onProgressUpdate(values);
            }
            /*
             * 功能:任务执行后
             */
            @Override
            protected void onPostExecute(LinearLayout result)
            {
                setProgressBarVisibility(true);
                layout1.addView(result);
                super.onPostExecute(result);
                
            }
            
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
  • 相关阅读:
    Java进阶之路——从初级程序员到架构师,从小工到专家
    成为JAVA架构师必看书籍推荐
    [Java]Spring Ioc讲解,不怕你不懂
    C#学习笔记(9)——委托(窗体传值)
    C#学习笔记(8)——委托应用(显示,写入时间)
    C#学习笔记(7)——委托
    C#学习笔记(6)——大项目增删改查
    C#学习笔记(5)——大项目查找
    C#学习笔记(4)——sqlserver常用语句
    C#学习笔记(3)——操作sqlserver数据库增删改查
  • 原文地址:https://www.cnblogs.com/txwtech/p/15983425.html
Copyright © 2020-2023  润新知