zoukankan      html  css  js  c++  java
  • Android Handler练习

     1 package com.example.myact12;
     2 
     3 
     4 import java.util.Random;
     5 
     6 import android.support.v7.app.ActionBarActivity;
     7 import android.os.Bundle;
     8 import android.os.Handler;
     9 import android.view.View;
    10 import android.widget.Button;
    11 import android.widget.TextView;
    12 /**
    13  * Android handler
    14  * @Describe: 
    15  * @package: com.example.myact12
    16  * @author shaobn
    17  * @date 2015-9-16 上午10:11:50
    18  */
    19 public class MainActivity extends ActionBarActivity {
    20     private Button start;
    21     private Button end;
    22     private TextView textView;
    23     private Handler handler = new Handler();
    24         
    25     @Override
    26     protected void onCreate(Bundle savedInstanceState) {
    27         super.onCreate(savedInstanceState);
    28         setContentView(R.layout.activity_main);
    29         start = (Button) this.findViewById(R.id.button1);
    30         end = (Button) this.findViewById(R.id.button2);
    31         textView = (TextView) this.findViewById(R.id.textView1);
    32         start.setOnClickListener(new ButtonClick());
    33         end.setOnClickListener(new ButtonClick());
    34     }
    35     class ButtonClick implements View.OnClickListener{
    36         @Override
    37         public void onClick(View arg0) {
    38             // TODO Auto-generated method stub
    39                 switch (arg0.getId()) {
    40                 case R.id.button1:
    41                     Runnable runnable = new Runnable() {
    42                         
    43                         @Override
    44                         public void run() {
    45                             // TODO Auto-generated method stub
    46                             Random random = new Random();
    47                             final int num = random.nextInt(100);
    48                             Runnable runnable2 = new Runnable() {
    49                                 
    50                                 @Override
    51                                 public void run() {
    52                                     // TODO Auto-generated method stub
    53                                     try {
    54                                         Thread.sleep(1000);
    55                                     } catch (Exception e) {
    56                                         // TODO: handle exception
    57                                         e.printStackTrace();
    58                                     }
    59                                     
    60                                     MainActivity.this.textView.setText(String.valueOf(num));
    61                                 }
    62                             };
    63                             
    64                             MainActivity.this.handler.post(runnable2);
    65                         }
    66                     };
    67                     new Thread(runnable).start();
    68                     break;
    69 
    70                 case R.id.button2:
    71                     Runnable runnable2 = new Runnable() {
    72                         
    73                         @Override
    74                         public void run() {
    75                             // TODO Auto-generated method stub
    76                             
    77                         }
    78                     };
    79                     break;
    80                 }
    81                 
    82             
    83         }
    84         
    85         
    86     }
    87 }
  • 相关阅读:
    marquee基本语法和marquee的相关参数设置
    [转]FreeTextBox使用详解
    div+css三级下拉菜单无限制下拉
    让Flash在Firefox和IE下背景透明
    asp.net制作幻灯片
    图片连续滚动代码,左右连续,上下连续不间断滚动
    纯DIV+CSS下拉菜单
    连续滚动图片代码
    sql语句修改access中的字段类型,access数据类型大全!
    非常棒的图片连续滚动代码
  • 原文地址:https://www.cnblogs.com/assassin666/p/4812381.html
Copyright © 2011-2022 走看看