随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
layout文件:
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.hanqi.testapp2.PractiseActivity4" 11 android:orientation="vertical"> 12 13 <TextView 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:id="@+id/tvw1"/> 17 <TextView 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:id="@+id/tvw2"/> 21 22 <Button 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:id="@+id/btt1" 26 android:text="随机选择" 27 android:onClick="btn_onClick"/> 28 </LinearLayout>
java类:
1 package com.hanqi.testapp2; 2 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatActivity; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.TextView; 8 import android.widget.Toast; 9 10 public class PractiseActivity4 extends AppCompatActivity { 11 12 TextView tvw1; 13 Button btt1; 14 TextView tvw2; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_practise4); 19 tvw1 = (TextView)findViewById(R.id.tvw1); 20 btt1 = (Button)findViewById(R.id.btt1); 21 tvw2 = (TextView)findViewById(R.id.tvw2); 22 } 23 String c1 = "北京"; 24 String c2 = "上海"; 25 public void btn_onClick(View v) 26 { 27 //创建子线程1 28 new Thread(){ 29 @Override 30 public void run() { 31 for (int i=0;i<20;i++) 32 { 33 if(i%2==0) 34 { 35 c1=""; 36 } 37 else 38 { 39 c1="北京"; 40 } 41 runOnUiThread(new Runnable() { 42 @Override 43 public void run() { 44 tvw1.setText(c1); 45 } 46 }); 47 //暂停 48 try { 49 Thread.sleep((int) (Math.random()*1000)); 50 } 51 catch (Exception e) 52 { 53 54 } 55 } 56 runOnUiThread(new Runnable() { 57 @Override 58 public void run() { 59 Toast.makeText(PractiseActivity4.this, c1 + "循环完成", Toast.LENGTH_SHORT).show(); 60 } 61 }); 62 } 63 }.start(); 64 //创建子线程2 65 new Thread(){ 66 @Override 67 public void run() { 68 for (int i=0;i<20;i++) 69 { 70 if(i%2==0) 71 { 72 c2=""; 73 } 74 else 75 { 76 c2="上海"; 77 } 78 runOnUiThread(new Runnable() { 79 @Override 80 public void run() { 81 tvw2.setText(c2); 82 } 83 }); 84 //暂停 85 try { 86 Thread.sleep((int) (Math.random()*1000)); 87 } 88 catch (Exception e) 89 { 90 91 } 92 } 93 runOnUiThread(new Runnable() { 94 @Override 95 public void run() { 96 Toast.makeText(PractiseActivity4.this, c2 + "循环完成", Toast.LENGTH_SHORT).show(); 97 } 98 }); 99 } 100 }.start(); 101 } 102 103 }
随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
时间: 2024-10-20 22:01:47