zoukankan      html  css  js  c++  java
  • 多线程编程--用接口方法实现选城市旅游

    package com.example.wang.myapplication;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.util.Random;
    
    public class ZuoyeActivity2 extends AppCompatActivity {
    
        Button bt_1;
        int j = 1;
        int k = 1;
        TextView tv_1;
        TextView tv_2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_zuoye2);
    
            bt_1 = (Button) findViewById(R.id.bt_1);
            tv_1 = (TextView) findViewById(R.id.tv_1);
            tv_2 = (TextView) findViewById(R.id.tv_2);
    
        }
        public void bt1_OnClick(View v)
        {
            j=k=1;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<10;i++)
                    {
                        On();
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for(int i=0;i<10;i++)
                    {
                        On1();
                    }
                }
            }).start();
        }
    
        public  void On()
        {
            Random random=new Random();
    
            runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
                    tv_1.setText("上海" + j);
                    j++;
                }
            });
    
            try
            {
                int t=random.nextInt(1000);
                Thread.sleep(t);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_1.setText("");
                }
            });
    
            try
            {
                int t=random.nextInt(1000);
                Thread.sleep(t);
                Log.e("TAG", "t=" + t);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        public  void On1()
        {
            Random random=new Random();
    
            runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
                    tv_2.setText("上海"+k);
                    k++;
                }
            });
    
            try
            {
                int t=random.nextInt(1000);
                Thread.sleep(t);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_2.setText("");
                }
            });
    
            try
            {
                int t=random.nextInt(1000);
                Thread.sleep(t);
                Log.e("TAG", "t=" + t);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
    }
    java
    <?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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.wang.myapplication.ZuoyeActivity"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#ccc"
            android:textSize="20sp"
            android:gravity="center"
            android:id="@+id/tv_1" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#cba"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_marginTop="5dp"
            android:id="@+id/tv_2"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="去哪旅游"
            android:id="@+id/bt_1"
            android:onClick="bt1_OnClick"/>
    
    </LinearLayout>
    xml

    效果图如上一条博客

  • 相关阅读:
    饿了么P7级前端工程师进入大厂的面试经验
    前端程序员面试的坑,简历写上这一条信息会被虐死!
    这次来分享前端的九条bug吧
    移动端开发必会出现的问题和解决方案
    创建一个dynamics 365 CRM online plugin (八)
    创建一个dynamics 365 CRM online plugin (七)
    创建一个dynamics 365 CRM online plugin (六)
    创建一个dynamics 365 CRM online plugin (五)
    使用User Primary Email作为GUID的问题
    怎样Debug Dynamics 365 CRM Plugin
  • 原文地址:https://www.cnblogs.com/wangchuanqi/p/5497375.html
Copyright © 2011-2022 走看看