Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RaceActivity.java
- ---------------------------------------------------------------------
- package com.example.teacher.threadproj;
- import android.content.Context;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.SeekBar;
- import com.example.teacher.threadproj.threads.HorseRunnable;
- public class RaceActivity extends AppCompatActivity {
- Context context;
- SeekBar seekHorse1;
- SeekBar seekHorse2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_race);
- setPointer();
- }
- private void setPointer() {
- context = this;
- seekHorse1 = findViewById(R.id.seek1);
- seekHorse2 = findViewById(R.id.seek2);
- Thread horseThread1 = new Thread(new HorseRunnable(context,seekHorse1,1));
- Thread horseThread2 = new Thread(new HorseRunnable(context,seekHorse2,2));
- horseThread1.start();
- horseThread2.start();
- }
- }
- ------------------------------------------
- HorseRunnable.java
- ------------------------------------------
- package com.example.teacher.threadproj.threads;
- import android.content.Context;
- import android.os.Handler;
- import android.widget.SeekBar;
- import android.widget.Toast;
- import java.util.Random;
- /**
- * Created by teacher on 23/05/2018.
- */
- public class HorseRunnable implements Runnable {
- private Context context;
- private SeekBar seekBar;
- private int id;
- public HorseRunnable(Context context,SeekBar seekBar, int id){
- this.context = context;
- this.seekBar = seekBar;
- this.id = id;
- this.seekBar.setProgress(0);
- this.seekBar.setMax(15);
- }
- @Override
- public void run() {
- for(int i=0; i<=seekBar.getMax(); i+=1){
- try {
- Thread.sleep(new Random().nextInt(500)+100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- seekBar.setProgress(i);
- }
- // android.os.Handler
- Handler handler = new Handler(context.getMainLooper());
- // handler can post code to execute on the ui thread. we get the ui thread by sending context.getMainLooper - the main (ui) thread
- // post notifies a thread that there is extra code to execute
- handler.post(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(context, "horse number "+id+" finished", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- -----------------------------------------------
- activity_race.xml
- ------------------------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:orientation="vertical"
- android:gravity="center"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/myRaceTtl"
- android:textAlignment="center"
- android:textSize="@dimen/ttl"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_margin="@dimen/seekMargin"
- >
- <SeekBar
- android:layout_weight="1"
- android:id="@+id/seek1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:progress="0"
- android:max="100"
- />
- <SeekBar
- android:layout_weight="1"
- android:id="@+id/seek2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:progress="0"
- android:max="100"
- />
- </LinearLayout>
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement