2017-2018-2 20165312 Experiment four "Android programming" experimental report install Android studio and install Android Studio with Hello World test and debug program
Can refer to Lou Teacher's blog Android Development Simple tutorial or reference "Java and Android Development Learning Guide" chapter 24th, there are detailed steps, step by step is very simple ~
After you create a new project project, the Project window has two main nodes:app and Gradle Scripts. The app node contains all the components in the application, and we use the app primarily when we write the program. There are 3 nodes under the app node
manifests
Contains a AndroidManifest.xml
manifest file
java
Contains all of the Java applications and test classes
res
Contains the resource files, and there are some directories in this directory:
drawable
layout
menu
values
mipmap
The above is a preliminary understanding of the construction of Android studio
Do Hello World test
Steps:
- Open it
layout->activity_main.xml
- Modify
android:text
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lenovo.myapplication.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Hello World! 20165312曹歌 Hello World! 20165311李嘉昕 Hello World! 20165313张晨晖" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
To debug a program log message
The simplest way to debug an application is to use log messages. During the development process, you can see the Android Ddms view at the bottom of the home screen of Android studio. With regard to LogCat, messages of different log levels are displayed in different colors, each message has a label, which makes it easy to find a message.
Tracking Program
- Set breakpoints on any line
- Open
Run->Debug
to Debug program
- Enter code, browse variables in the Debug bar below Android Studio
- Specific debug code method operations personally think is the same as the idea debug code method ~
Second, activity testing
Activity is a window that contains a user interface component, a typical Android application that starts with an activity. The first window created by an application is called the main activity.appliaction元素定义两个活动,其中之一使用intent-filter元素声明的为主活动。
Starting an activity involves instantiating the active class and invoking the life cycle method:
onCreat()
onStart()
onResume()
onPause()
onStop()
onRestart()
onDestory()
Each control file Activity requires a corresponding startup program file (. java) and the corresponding layout file (. xml)
Build the project, run the textbook related code
Link to the relevant code of the textbook
Create thirdactivity, display your own number in thirdactivity, modify the code to let mainactivity start thirdactivity
Steps:
In the AndroidManifest.xml
manifest file, add aactivity
<activity android:name=".ThirdActivity"> </activity>
Add a ThirdActivity.java
file
package cn.edu.besti.is.cg;import android.app.Activity;import android.os.Bundle;public class ThirdActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third);}}
Add a third_activity.xml
file
<RelativeLayout 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: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=".ThirdActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="20165312曹歌"/></RelativeLayout>
- Modify
MainActivity.java
to make it self-startingThirdActivity.java
Package Cn.edu.besti.is.cg;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import android.widget.textview;import static cn.edu.besti.is.cg.r.id.textview1; public class Mainactivity extends Activity implements Ontouchlistener {@Override protected void onCreate (Bun Dle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView TV = (TextView) Findviewbyid (TEXTVIEW1); Tv.setontouchlistener (this); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it//is present. Getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override public boolean OnTouch (View arg0, motionevent event) {Intent Intent = new Intent (This, thirDactivity.class); Intent.putextra ("message", "Message from First screen"); StartActivity (Intent); return true; }}
Third, UI testing
The so-called UI, is to build the user cross for the main activity (users interface)
Modify the code to let the toast message display its own learning number information
Toast: A consumer pop-up dialog box that displays a message as feedback to the user, and toast does not replace the current content.
Modify Activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.edu.besti.is.cg.ui.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="欢迎来到20165312的世界" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>
Modify Mainactivity.java
package cn.edu.besti.is.cg.ui;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Toast;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast toast = Toast.makeText(MainActivity.this, "20165312曹歌", Toast.LENGTH_LONG); toast.show(); }}
Four, layout test
Some of the layouts in Android:
LinearLayout
A layout that aligns all the child views in the same direction (horizontal or vertical)
RelativeLayout
Arranges one of his layouts according to the position of one or more sibling views of the child view
FrameLayout
A layout that places each child view at the top of another child view
TableLayout
A layout that organizes a child view by rows and columns
GridLayout
A layout that places a child view in a grid
Modify the layout so that the P290 page interface is different from the textbook
Modifyactivity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginLeft="100dp" /> <ImageButton android:src="@android:drawable/btn_star_big_on" android:alpha="0.45" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="200dp" android:layout_marginLeft="300dp" /></FrameLayout>
V. Event processing test run textbook code
Mainactivity.java
Package Cn.edu.besti.is.cg.cellview;import android.app.activity;import Android.graphics.color;import Android.os.bundle;import Android.view.menu;import Android.view.view;public class Mainactivity extends Activity {int Co unter = 0; int[] Colors = {color.black, Color.Blue, Color.cyan, Color.dkgray, Color.gray, Color.green, Color.ltgray, Color.magenta, Color.Red, Color.White, color.yellow}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action Bar if it//is present. Getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; public void ChangeColor (view view) {if (counter = = colors.length) {counter = 0; } view.setbackgroundcolor (colors[counter++]); }}
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: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=".MainActivity"> <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="90dp" android:id="@+id/analogClock1" android:onClick="changeColor" /></RelativeLayout>
The problems encountered in the experiment appeared
Executing tasks: app:assembleDebug
The problem, after Baidu found Androidstudio appeared executing tasks:app:assembleDebug.
build.gradle(module:app)
in the file
testApplicationId“com.cn.skypiea.test" testInstrumentationRunner "android.test.InstrumentationTestRunner"
After you have commented, you can package it successfully.
Appear
Execution failed for task ‘:app:preDebugAndroidTestBuild
The problem, Baidu found a blog post,
Build->Rebuild Project
You can then run the problem of R Red
In fact, I think R is very iffy, I have tried several solutions, the first time I rerun and then R is automatically not marked red, anyway is very strange; the second time I changed package
the content of the red and then it is OK, and the third time I took a Ctrl+Enter
new thing on the right, so more try ...
Finally I found a blog in Android studio R into Red Studio R into red for everyone's reference.
2017-2018-2 20165312 Experimental Report on Android program design