20165310 experiment Four "android program design" 24th: The first knowledge of Android task one: rewrite the res directory content, Hello world to show their own school number, before and after a classmate's school number
First we need to understand the project structure of Android Studio, where the content we want to edit is set in the app catalog.
build: Files that are stored in the project are build automatically generated.
libs: The libraries that are introduced are generally stored in libs .
src: The program files written by the developer are saved in the SRC directory, and our main learning content is also in this directory.
AndroidManifest.xml: This is a control file that is used to describe the application. Different parameters represent different meanings, such as "manifest" as the root node, which describes everything in the package; the "Application" element can contain some global and default properties of application, such as tags, icons, themes, and so on; " Activity "is the primary tool for interacting with users, typically containing one or more activity elements that describe various activities in the app. All new activities must be declared, otherwise they will not work.
javaDirectory: The source code that holds the activity, all the active programs are created in this directory.
resDirectory: Stores the various resources used by the application, such as XML interface files, pictures, data, etc. Usually contains the drawable subdirectory, the layout subdirectory, and the values sub-directory.
drawable: Store images with different resolutions, app icons, etc.
layout: Storing XML interface layout files, mainly used to display user interface, the Java folder of the active XML file created in this directory.
values: Stores different types of data, such as String, array, and so on.
For this task, we need to res layout modify the activity_main.xml corresponding content in-box:
android:text="Hello World!"Change android:text="Hello World!\n20165310\n2065310\n20165311" it to a running result as shown:
The 25th Chapter: The activity creates the thirdactivity, displays own school number in the thirdactivity, modifies the code to let mainactivity start thirdactivity
- To create an activity
ThirdActivity :
?```javapackage com.example.abc.a20165310exp4_2;import android.support.v7.app.AppCompatActivity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class ThirdActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); }}?```
-
Modification MainActivity , the use of intent related functions, so that the main function can trigger ThirdActivity , the new OnTouch method is as follows:
@Override publicbooleanonTouch(View arg0, MotionEvent event) { newIntent(this, ThirdActivity.class); startActivity(intent); returntrue; }
-
AndroidManifest.xmlregistering for the event:
<activity android:name=".ThirdActivity" > </activity>
-
activity_third.xmlto create and configure:
<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/textview"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:text="20165310"/></relativelayout>
The results of the operation are as follows:
26th: UI Component Task Three: Modify the code to let the toast message display its own school number information
-
Toast: Toast is a mechanism used to display information in Android. Toast shows a limited amount of time and automatically disappears after a period of time and does not occupy any memory. ToastThere are many usages, such as default display, custom display location, display with picture, full custom display, other thread invocation display, and so on. The most commonly used default displays are shown here.
-
MainActivityedit to make changes:
Package Com.example.abc.a20165310exp4_3;import android.support.v7.app.AppCompatActivity;import Android.os.Bundle;import android.view.Gravity;import Android.view.View;import Android.widget.Button;import Android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout;import Android.widget.TextView;import Android.widget.Toast; Public classMainactivityextendsappcompatactivity {@Override protected void onCreate(Bundle savedinstancestate) {Super.onCreate(savedinstancestate);Setcontentview(R.Layout.Activity_main); Button Btnshow1 = (button)Findviewbyid(R.ID.SS1); Btnshow1.Setonclicklistener(NewView.Onclicklistener() {@Override Public void OnClick(View v) {Toast toast = toast.}Maketext(Mainactivity. This,"20165310", Toast.Length_long); Toast.Show(); } }); }}
-
To activity_main.xml make edits:
<?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:layout_gravity="Center"android:gravity="Center_horizontal"android:padding="120DP"android:orientation="Vertical"tools:context=". Mainactivity "> <button android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_gravity="Right"android:layout_margintop="16DP"android:paddingleft="16DP"android:paddingright="16DP"android:text="20165310xw"Android:id="@+id/ss1"/></linearlayout>
-
The results of the operation are as follows:
The 27th chapter: Layout Task Four: Modify the layout to make the interface of P290 page different from the textbook
- The Frame layout container creates an empty area for each component, a region called a frame that is automatically aligned according to the gravity attribute defined in Framelayout.
- We need to add the Layout_gravity property to the component to customize the alignment of the build. If you do not use the Layout_gravity property, multiple content overlaps.
layout_gravityYou can use the values shown below:
top: Places the object at the top of its container without changing its size;
bottom: Place the object at the bottom of its container without changing its size;
left: Place the object on the left side of its container without changing its size;
certer_vertical: Center The object vertically, without changing its size, and align horizontally in the vertical direction;
- ......
The modified xml code is as follows:
?
<?xml version="1.0"encoding="Utf-8"? ><framelayout 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:orientation="Horizontal"Android:layout_width="Match_parent"android:layout_height="Match_parent"> <button android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_marginleft="150DP"android:layout_margintop="150DP"android:text="20165310"/> <imagebutton android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_marginleft="30DP"android:layout_margintop="60DP"Android:alpha="0.45"Android:src="@android:d rawable/alert_dark_frame"/> <imagebutton android:id="@+id/imagebutton"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"app:srccompat="@android: Color/holo_orange_dark"/> <imagebutton android:id="@+id/imagebutton2"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"app:srccompat="@android:d Rawable/btn_star"/></framelayout>
The results of the operation are as follows:
28th: Listener Task Five: Run the textbook this chapter related code and
- Android is event-based. User interaction with one view in an activity may trigger an event, including clicks, long presses, touches and keystrokes, and so on.
- To have a program respond to an event, you need to write a listener for the event. That is, to implement
android.view.View an interface embedded in a class. such as OnClickListener the method of the interface onClick() .
When the user presses (or touches) the clock, the method is called and the clock object is accepted. To modify the color of the clock, you need to call its setBackgroundColor method, pass in a Color object, and make the touch clock change color. Add an Android icon with five pentagram, click on the pentagram and the pentagram will start to brighten from the first until full.
-
MainActivityedit to make changes:
Package com.example.abc.a20165310exp4_5;import android.support.v7.app.AppCompatActivity;import android.app.Activity;import Android.graphics.Color;import Android.os.Bundle;import Android.view.Menu;import Android.view.View; Public classMainactivityextendsActivity {intCounter =0;int[] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.YELLOW, Color.Dkgray, Color.GRAY, Color.GREEN, Color.Ltgray, Color.MAGENTA, Color.RED, Color. White, Color.YELLOW, Color.GREEN};@Override protected void onCreate(Bundle savedinstancestate) {Super.onCreate(savedinstancestate);Setcontentview(R.Layout.Activity_main); } Public void ChangeColor(View view) {if(Counter = = colors.length) {counter =0; } view.SetBackgroundColor(colors[counter++]); }}
-
To activity_main.xml make edits:
<?xml version="1.0"encoding="Utf-8"? ><relativelayout 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"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:id="@+id/analogclock1"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:layout_alignparentbottom="true"Android:layout_centerhorizontal="true"Android:layout_marginbottom="195DP"android:onclick="ChangeColor"/> <ratingbar android:layout_width="Wrap_content"android:layout_height="Wrap_content"Android:layout_alignparentbottom="true"Android:layout_centerhorizontal="true"Android:layout_marginbottom="82DP"/> <imageview android:id="@+id/imageview"Android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:layout_alignparenttop="true"Android:layout_centerhorizontal="true"android:layout_margintop="144DP"Android:src="@mipmap/ic_launcher"/></relativelayout>
-
The results of the operation are as follows:
Experimental Thoughts
- The process of installing Android Studio has been a lot of difficulty, and finally the customer service, the process of learning a lot of knowledge.
- Although Android development is based on Java, but whether it is from the structure of project or the first contact with Reg, there are a lot of new content to learn, this experiment has urged our theoretical study also consolidated the theoretical knowledge.
20165310 Java experiment four "Android programming"