随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.xuexi.TestActivity4"
11     android:orientation="vertical">
12 
13     <Button
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="开始"
17         android:id="@+id/bt_1"
18         android:onClick="bt_OnClick"/>
19 
20 </LinearLayout>
.xml
 1 package com.example.wang.xuexi;
 2 
 3 import android.app.AlertDialog;
 4 import android.content.DialogInterface;
 5 import android.support.annotation.RequiresPermission;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.os.Bundle;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.TextView;
12 
13 import java.util.Random;
14 
15 public class TestActivity4 extends AppCompatActivity {
16 
17     Button bt_1;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_test4);
23 
24         bt_1=(Button)findViewById(R.id.bt_1);
25 
26     }
27 
28     Random random=new Random();
29 
30     public void bt_OnClick(View v) {
31 
32         for (int i=0;i<10;i++)
33         {
34             new Thread(){
35                 @Override
36                 public void run() {
37                     try{
38 
39                         Thread.sleep(random.nextInt(2000));
40                     }catch (Exception e){
41 
42                     }
43                     runOnUiThread(new Runnable() {
44                         @Override
45                         public void run() {
46 
47                             bt_1.setText("济南");
48                         }
49                     });
50                     return;
51                 }
52             }.start();
53 
54             new Thread(new Runnable() {
55                 @Override
56                 public void run() {
57                     try {
58                         Thread.sleep(random.nextInt(2000));
59                     }catch (Exception e){
60 
61                     }
62                     runOnUiThread(new Runnable() {
63                         @Override
64                         public void run() {
65 
66                             bt_1.setText("淄博");
67                         }
68                     });
69                     return;
70                 }
71             }).start();
72         }
73 
74     }
75 }
.java

posted on 2016-05-16 00:47  安然罒  阅读(123)  评论(0编辑  收藏  举报