Android笔记之——线程

实践是知识的母亲,知识是生活的明灯。这篇文章主要讲述Android笔记之——线程相关的知识,希望能为你提供帮助。
【Android笔记之——线程】java:

package com.example.test_ui_thread01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//为button1添加一个动画操作
Button button1 = (Button)findViewById(R.id.button1);
TranslateAnimation animation = new TranslateAnimation(0,150,0,0);
animation.setRepeatCount(30);
animation.setDuration(2000);
button1.setAnimation(animation);

Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();


}
});
}
}
XML:

< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.test_ui_thread01.MainActivity">

< TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />

< Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="29dp"
android:layout_marginStart="29dp"
android:layout_marginTop="97dp"
android:id="@+id/button1" />

< Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_marginTop="81dp"
android:id="@+id/button2" />
< /RelativeLayout>




















































































    推荐阅读