赞
踩
Android环境构建与Activity生命周期
JDK1.8,Android Studio
安装智能手机开发相关软件平台,并在此基础上测试Activity的生命周期过程。
- //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">
-
-
-
- <TextView
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:text="Hello World!"
-
- app:layout_constraintBottom_toBottomOf="parent"
-
- app:layout_constraintLeft_toLeftOf="parent"
-
- app:layout_constraintRight_toRightOf="parent"
-
- app:layout_constraintTop_toTopOf="parent" />
-
-
-
- </androidx.constraintlayout.widget.ConstraintLayout>

- //MainActivity.java
-
- package com.example.helloworld;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
-
-
- public class MainActivity extends Activity {
-
- public MainActivity(){
- Log.e("TAG","MainActivity()");
- }
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- Log.e("TAG","onCreate()");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- @Override
- protected void onStart() {
- Log.e("TAG","onStart()");
- super.onStart();
-
- }
- @Override
- protected void onResume() {
- Log.e("TAG","onResume()");
- super.onResume();
-
- }
-
-
- @Override
- protected void onPause() {
- Log.e("TAG","onPause()");
- super.onPause();
- }
-
- @Override
- protected void onStop() {
- Log.e("TAG","onStop()");
- super.onStop();
- }
-
- @Override
- protected void onDestroy() {
- Log.e("TAG","onDestroy()");
- super.onDestroy();
- }
-
- @Override
- protected void onRestart() {
- Log.e("TAG","onRestart()");
- super.onRestart();
- }
- }
-
-

1.此时安装好的JDK版本
2、3.能够创建一个新工程项目helloworld且能运行,在Android studio中能够改变主题和字体样式和大小,此时我选择默认不做改变
4.(1)首次打开helloworld查看运行周期日志
(2)点击back,查看周期日志
(3)重新启动,查看周期日志
(4)点击home,查看周期日志
能够安装好JDK,收悉了android studio各个工程项目中的文件,能够初次运行helloworld,且学习到了app的生存周期并且能够查看日志。
安装智能手机开发相关软件平台,并在此基础上测试Activity的生命周期过程。
5、bug调试、测试实验
以下代码是实现一个进度条测试程序,在界面(activity_main.xml)上放置一个水平长条形的进度条(style:progressBarStyleHorizontal,id:horizontal_pbar),以及一个启动进度条的按钮。点击按钮,进度条从0增加到满格100,进度条停止。由于代码中存在各种问题,现请你将代码修正和调试正确。
- //activity_main.xml
-
- <?xml version="1.0" encoding="utf-8"?>
-
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- xmlns:tools="http://schemas.android.com/tools"
-
- android:layout_width="match_parent"
-
- android:layout_height="match_parent"
-
- android:orientation="vertical"
-
- tools:context="com.example.helloworld.MainActivity">
-
- <ProgressBar
-
- android:id="@+id/horizontal_pbar"
-
- style="?android:attr/progressBarStyleHorizontal"
-
- android:layout_width="match_parent"
-
- android:layout_height="50dp"
-
- android:max="100" />
-
- <Button
-
- android:id="@+id/start_pbar_btn"
-
- android:layout_width="match_parent"
-
- android:layout_height="wrap_content"
-
- android:text="启动进度条" />
-
- </LinearLayout>

- //MainActivity.java
-
- package com.example.helloworld;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ProgressBar;
-
- public class MainActivity extends AppCompatActivity {
- private ProgressBar mProgress;
- private Handler mHandler = new Handler();
- private Boolean FLAG=true;
-
- private Button startpbarbtn;
-
- int mProgressStatus=0;
- protected void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.activity_main);
- mProgress = (ProgressBar) findViewById(R.id.horizontal_pbar);
- //设置监听按钮
- startpbarbtn = (Button)findViewById(R.id.start_pbar_btn);
- //设置监视内容
- startpbarbtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
-
- //监听
- new Thread(new Runnable() {
- public void run() {
- //推荐使用标志位使其不执行代码,不推荐使用true
- while (FLAG) {
- mProgressStatus = addNum();
- // 更新进度条
- // post方法推送一个Runnable到Handler,可以在此更新UI
- mHandler.post(new Runnable() {
- public void run() {
- if(mProgressStatus<100) {
- mProgress.setProgress(mProgressStatus);
- mProgressStatus++;
- }
- //执行完成后隐藏进度条并将标志设置为false
- else {
- mProgress.setVisibility(View.INVISIBLE);
- FLAG = false;
- }
- }
- });
- }
- }
- private int addNum() {
-
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return mProgressStatus;
- }
-
- }).start();
- }
- });
- }
- }

1.将bug程序修改完成,启动
2.点击启动进度条,开始启动
3.进度完成,隐藏进度条
对部分代码进行修改,增加了按钮监听,使得程序可以正常运行,对监听器的运用和Android studio代码的编写有了进一步的了解和运用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。