目錄
- 實驗題目
- 實驗一
- 代碼:
- 布局檔案:
- 實驗二
- 代碼:
- 布局檔案:
實驗題目
- 實作一個倒計時時鐘,創建子線實作計時功能,使用異步訊息機制將計時結果顯示在界面上,如圖,在文本框中輸入數字(使用Number文本框),點擊開始按鈕后,下方文本框的數字每秒減1,點擊停止即停止倒計時
- 創建ClockActivity,可輸入一個時間,再創建一個ClockService在用于計時,到時間后,以在Activity中發出通知(在下方的TextView中顯示“時間到”),
注意:這里涉及到了Service操作Activity
實驗一
代碼:
MainActivity :
public class MainActivity extends AppCompatActivity {
boolean iswork;
String number;
EditText edtNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart=(Button)findViewById(R.id.btnStart);
Button btnStop=(Button)findViewById(R.id.btnStop);
final Handler handler=new Handler(){
public void handleMessage(Message msg){
edtNumber=(EditText)findViewById(R.id.edtNumber);
edtNumber.setText(String.valueOf(msg.what));
}
};
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText edtCount=(EditText)findViewById(R.id.edtNumber);
number=String.valueOf(edtCount.getText());
iswork=true;
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i=Integer.valueOf(number)-1;i>=0;i--){
if (iswork){
//間隔一秒
Thread.sleep(1000);
Message msg=new Message();
msg.what=i;
handler.sendMessage(msg);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (iswork){
//停止
iswork=false;
}
}
});
}
}
布局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="150dp"
android:layout_marginLeft="15dp"
android:layout_height="wrap_content"
android:onClick="Camera_onClick"
android:id="@+id/btnStart"
android:text="開始"></Button>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:onClick="Album_onClick"
android:id="@+id/btnStop"
android:text="停止"></Button>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="100"
android:textSize="150sp"
android:id="@+id/edtNumber"
android:background="#D3D3D3"
android:textAlignment="center"></EditText>
</LinearLayout>
實驗二
代碼:
ClockActivity :
public class ClockActivity extends AppCompatActivity {
private TextView tvClock;
public static final String CLOCK_ACTION="com.lcl.test8.Clock_Action";
public static int TIME=0;//倒計時的時間
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock);
tvClock=(TextView)super.findViewById(R.id.tvClock);
//注冊廣播
regReceiver();
}
private void regReceiver(){
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction(CLOCK_ACTION);
super.registerReceiver(clockReceiver, intentFilter);
}
//廣播接受者,接受來自ClockService(計時服務)的廣播,ClockService每隔一秒鐘發一次廣播
private BroadcastReceiver clockReceiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
changeTime();//改變TextView中的顯示時間
}
};
//點擊Button確定 開始發送廣播,開始計時
public void StartTimeOnclick(View view){
EditText editTime = (EditText)findViewById(R.id.inputTime);
//獲取輸入EditText的時間
String inputTime = String.valueOf(editTime.getText());
//將獲取的字串時間用":"分割為時,分,秒
String[] str =inputTime.split("[:]");
int hours = Integer.valueOf(str[0]).intValue();
int minutes = Integer.valueOf(str[1]).intValue();
int seconds = Integer.valueOf(str[2]).intValue();
TIME=hours*60*60*1000+minutes*60*1000+seconds*1000;
//啟動計時服務
startService(new Intent(this,ClockService.class));
}
//時間展示動態變化
private void changeTime(){
String timeStr="";
if(TIME==0){
timeStr="時間到!";
}else{
int hour=TIME/(1000*60*60);
int minute=TIME%(1000*60*60)/(60*1000);
int second=(TIME%(1000*60*60))%(60*1000)/1000;
String hourStr = String.valueOf(hour);
String minuteStr =String.valueOf(minute);
String secondStr = String.valueOf(second);
if(hour<=9){
hourStr="0"+hour;
}
if(minute<=9){
minuteStr="0"+minute;
}
if (second<=9){
secondStr="0"+second;
}
timeStr= hourStr+":"+ minuteStr+ ":"+ secondStr;
}
tvClock.setText(timeStr);
}
}
ClockService :
public class ClockService extends Service {
public ClockService() {
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
countTime();//執行計時功能
return super.onStartCommand(intent, flags, startId);
}
//實作計時功能,每隔一秒減少總時間并MainActivity發送廣播
private void countTime() {
new Thread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(ClockActivity.CLOCK_ACTION);
while (true) {
try {
Thread.sleep(1000);
if (ClockActivity.TIME <= 0) {
sendBroadcast(intent);
break;
}
ClockActivity.TIME -= 1000;
sendBroadcast(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
布局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<EditText
android:layout_width="200dp"
android:layout_height="80dp"
android:id="@+id/inputTime"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:text="輸入時間"
android:textAlignment="center"></EditText>
<Button
android:layout_width="150dp"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:textSize="30dp"
android:text="確定"
android:onClick="StartTimeOnclick"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="400dp">
<TextView
android:layout_width="380dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="#BDBDBD"
android:text="計時"
android:textSize="75dp"
android:id="@+id/tvClock"
android:gravity="center">
</TextView></RelativeLayout>
</LinearLayout>
參考博文:
https://blog.csdn.net/h2503652646/article/details/86471273
https://blog.csdn.net/weixin_33913377/article/details/93254934
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/208847.html
標籤:其他
上一篇:理解 OC 中 RunLoop
下一篇:Android權限大全
