首页 > 技术文章 > 5月16日课程总结

TENOKAWA 2016-05-16 21:01 原文

1.AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.administrator.testapp">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:supportsRtl="true"
10         android:theme="@style/AppTheme">
11         <activity android:name=".MainActivity"></activity>
12         <activity android:name=".test_activity6">
13             <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
15                 <category android:name="android.intent.category.LAUNCHER" />
16             </intent-filter>
17         </activity>
18     </application>
19 
20 </manifest>

2.Activity_test_activity6.xml

【城市选择程序 界面代码】

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.administrator.testapp.test_activity6"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="城市一"
        android:textSize="30dp"
        android:id="@+id/tv_3"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="城市二"
        android:textSize="30dp"
        android:id="@+id/tv_4"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="城市三"
        android:textSize="30dp"
        android:id="@+id/tv_5"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动"
        android:onClick="bt1_onclick"/>
<LinearLayout/>

 【城市选择运行代码】

  1 package com.example.administrator.testapp;
  2 
  3 import android.os.Message;
  4 import android.support.v7.app.AppCompatActivity;
  5 import android.os.Bundle;
  6 import android.view.View;
  7 import android.os.Handler;
  8 import android.widget.Button;
  9 import android.widget.TextView;
 10 import android.widget.Toast;
 11 import java.util.Random;
 12 import java.util.logging.LogRecord;
 13 
 14 public class test_activity6 extends AppCompatActivity {
 15 
 16     TextView tv_3,tv_4,tv_5,tv_6;
 17 
 18 @Override
 19     protected void onCreate(Bundle savedInstanceState) {
 20         super.onCreate(savedInstanceState);
 21         setContentView(R.layout.activity_test_activity6);
 22         tv_3 = (TextView) findViewById(R.id.tv_3);
 23         tv_4 = (TextView) findViewById(R.id.tv_4);
 24         tv_5 = (TextView) findViewById(R.id.tv_5);
 25 
 26     String c1 = "北京";
 27     String c2 = "香港";
 28     String c3 = "重庆";
 29 
 30     public void bt1_onclick(View v)
 31     {
 32         new Thread()
 33         {
 34             @Override
 35             public void run() {
 36                 for (int i=0;i<20;i++)
 37                 {
 38                     if (i%2==0)
 39                     {
 40                         c1="";
 41                     }
 42                     else
 43                     {
 44                         c1="北京";
 45                     }
 46                     runOnUiThread(new Runnable() {
 47                         @Override
 48                         public void run() {
 49                             tv_3.setText(c1);
 50                         }
 51                     });
 52                     try
 53                     {
 54                         Thread.sleep((int)(Math.random()*1000));
 55                     }
 56                     catch (Exception e)
 57                     {
 58 
 59                     }
 60                 }
 61                 runOnUiThread(new Runnable() {
 62                     @Override
 63                     public void run() {
 64                         Toast.makeText(test_activity6.this, c1+"循环完成", Toast.LENGTH_SHORT).show();
 65                     }
 66                 });
 67             }
 68         }.start();
 69 
 70         //创建子线程2
 71         new Thread()
 72         {
 73             @Override
 74             public void run() {
 75                 for (int i=0;i<20;i++)
 76                 {
 77                     if (i%2==0)
 78                     {
 79                         c2="";
 80                     }
 81                     else
 82                     {
 83                         c2="香港";
 84                     }
 85                     runOnUiThread(new Runnable() {
 86                         @Override
 87                         public void run() {
 88                             tv_4.setText(c2);
 89                         }
 90                     });
 91                     try
 92                     {
 93                         Thread.sleep((int)(Math.random()*1000));
 94                     }
 95                     catch (Exception e)
 96                     {
 97 
 98                     }
 99                 }
100                 runOnUiThread(new Runnable() {
101                     @Override
102                     public void run() {
103                         Toast.makeText(test_activity6.this, c2+"循环完成", Toast.LENGTH_SHORT).show();
104                     }
105                 });
106             }
107         }.start();
108         //创建子线程
109         new Thread()
110         {
111             @Override
112             public void run() {
113                 for (int i=0;i<20;i++)
114                 {
115                     if (i%2==0)
116                     {
117                         c3="";
118                     }
119                     else
120                     {
121                         c3="重庆";
122                     }
123                     runOnUiThread(new Runnable() {
124                         @Override
125                         public void run() {
126                             tv_5.setText(c3);
127                         }
128                     });
129                     try
130                     {
131                         Thread.sleep((int)(Math.random()*1000));
132                     }
133                     catch (Exception e)
134                     {
135 
136                     }
137                 }
138                 runOnUiThread(new Runnable() {
139                     @Override
140                     public void run() {
141                         Toast.makeText(test_activity6.this, c3+"循环完成", Toast.LENGTH_SHORT).show();
142                     }
143                 });
144             }
145         }.start();
146     }
147 }

推荐阅读