Demo 1a751 EditGreeting Hello World Click me! 11
Demo 11
Create a project,and open the layout xml file to make the layout as follows Hellolava java MsinActivityjava activity_main.xml Palette Palette Nexus One AppTheme (activityClass)19 Form Widgets 回国图国回图 四TextView Ab Large Text 令 AMedium Text 四Small Text☒Button EditGreeting TextView e☒Small Button ▣ToggleButton ☑CheckBox RadioButton ◆Hello world! CheckedTextView 回Spinner EditText ☐ProgressBar(Larg9 ☐ProgressBar(Normal) ☐Progress8ar(Smal0 ProgressBar (Horizontal) SeekBar ☐OuickContactRador □Text Fields Button Click me! □Layouts ☐Composite ☐Images&Media □Time&Date Transitions □Advanced □Other ☐Custom&Library Views ▣Graphical Layout activity.main.xml <terminated>Hellolava (1)[Java Application]C\Program Files ava\jre8\bin\javaw.exe (04/08/2014 8:05:56 PM) Hello Java! 12
12 Create a project, and open the layout xml file to make the layout as follows. TextView EditText Button
<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" tools:context="SfrelativePackage).$factivityclass)"> <Button android:id="@+id/myButton" android:layout width="wrap_content" Create an id for the Button, android:layout height="wrap content" android:layout_alignParentBottom="true" EditText and TextView, android:layout_centerHorizontal="true" android:layout marginBottom="122dp" respectively. android:text="CLick me!"/> <EditText This is by specifying the android:id="@+id/myEditText" android:layout_width="match_parent" android:id attribute,with android:layout_height="wrap_content" android:layout_above="+id/myButton" formate“@+id/anld" android:layout_alignParentLeft="true" android:layout_marginBottom="91dp" android:ems="10"> You can arbitrarily name the <requestFocus / id,but make sure that the id </EditText> is unique <TextView android:id="+id/myTextview" android:layout_width="wrap_content" android:layout_height="wrap content" android:layout_above="@+id/myEditText" android:layout_centerHorizontal="true" android:layout_marginBottom="60dp" android:text="@string/hello_world"/ </RelativeLayout> 13
13 Create an id for the Button, EditText and TextView, respectively. This is by specifying the android:id attribute, with formate “@+id/anId” You can arbitrarily name the id, but make sure that the id is unique
Open the MainActivity.java file in the java directory.The codes look like below. Android -r©MainActivity.java×activity_main.xml× app ☐manifests package com.example.toml.editgreeting; ▣java com.example.toml.editgreeting import... cMainActivity com.example.toml.editgreeting (androidTest) V□s drawable public class MainActivity extends Activity 7▣layout activity_main.xml menu COverride p▣mipmap protected void onCreate(Bundle savedInstanceState){ p▣values Gradle Scripts super.onCreate(savedInstanceState); setContentView(R.layout.activity_main): This is created automatically by the template.You may have more codes over there.If that is the case,no worries.You probably just adopt another template and it is also going to work. In the next slides,we are going to elaborate on the codes
14 Open the MainActivity.java file in the java directory. The codes look like below. This is created automatically by the template. You may have more codes over there. If that is the case, no worries. You probably just adopt another template and it is also going to work. In the next slides, we are going to elaborate on the codes
This defines a package,and include your MainActivity class in the package. My project is named as EditGreeting.So the package name is com.example.editgreeting.You may have different package name here as you use another project name. Can you roughly guess the directory of your MainActivity.java file based on the package name? package com.example.editgreeting; import android.app.Activity; public class MainActivity extends Activity @Override protected void onCreate(Bundle savedInstancestate){ super.onCreate(savedInstanceState); setContentview(R.layout.activity_main); 2 Do you have an idea why we use a reversed order of the organization for the package name? 15
15 This defines a package, and include your MainActivity class in the package. My project is named as EditGreeting. So the package name is com.example.editgreeting. You may have different package name here as you use another project name. Can you roughly guess the directory of your MainActivity.java file based on the package name? Do you have an idea why we use a reversed order of the organization for the package name?