这个是比较简单的,涉及到了网络编程,代码如下:
MainActivity:
package cn.edu.bzu.imagebrowser;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private TextView tvNumber;
private int count = 10;
public static final int UPDATA_TIME = 1;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case UPDATA_TIME:
tvNumber.setText(count + "");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvNumber = (TextView) findViewById(R.id.tvNumber);
}
public void start(View view) {
new Thread(new Runnable() {
@Override
public void run() {
while (count > 0) {
count--;
Message message = new Message();
message.what = UPDATA_TIME;
handler.sendMessage(message);
//tvNumber.setText(count+"");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
activity_main.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="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:layout_marginLeft="10dp"
tools:context="cn.edu.bzu.imagebrowser.MainActivity"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvNumber"
android:text="10"
android:textSize="100sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:text="start"
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:id="@+id/button"
android:onClick="start" />
</RelativeLayout>