今天学习了秒表app的制作:
MainActivity:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/timeview" android:layout_width="match_parent" android:layout_height="47dp" android:gravity="center_horizontal" android:text="0:00:00" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickStart" android:text="@string/start" /> <Button android:id="@+id/button_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickStop" android:text="@string/stop" /> <Button android:id="@+id/button_reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickReset" android:text="@string/reset" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/timeview" android:layout_width="match_parent" android:layout_height="47dp" android:gravity="center_horizontal" android:text="0:00:00" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickStart" android:text="@string/start" /> <Button android:id="@+id/button_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickStop" android:text="@string/stop" /> <Button android:id="@+id/button_reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickReset" android:text="@string/reset" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
strings.xml:
<resources> <string name="app_name">Stopwatch</string> <string name="start">Start</string> <string name="stop">Stop</string> <string name="reset">Reset</string> </resources>
遇到的问题:
有一个小bug:旋转屏幕改变了设备配置device configuration
设备配置包括:系统语言、屏幕大小、屏幕方向、是否外接键盘改设备配置会导致Activity被销毁后新建