Any programming learning start is HelloWorld, as a slight >net programming experience for us to skip this step, we start with a simple login interface. First look at the effect:
First, the preparation of knowledge:
1. Android Environment: Install the JDK, directly to the official website to download the Adt-bundle integration package after the update can be used.
2. Project directory: A picture shows everything
Second, the page layout:
Or a picture that shows everything
So what about the layout of this interface?
<LinearLayout>The outermost div, with a linear layout, is oriented vertically<TextView/>is "Initial user name ... "These words are where lable<LinearLayout>inside the div, horizontal layout<TextView/>User name lable<EditText/>User name textbox</LinearLayout> <LinearLayout>inside the div, horizontal layout<TextView/>Password lable<EditText/>Password textbox</LinearLayout> <Button/>Login Button</LinearLayout>
A look, will be half, down one by one look:
1. Outermost div:
<xmlns:android= "http://schemas.android.com/apk/res/android"android: Layout_width= "Fill_parent"android:layout_height= "Fill_parent"android:o Rientation= "vertical" android:layout_margin= "5dip">
Defines a wide-height docking style, with
fill_parent, Match_parent: is the same, in order to be compatible with the lower version, it is recommended to use Fill_parent
Setting the layout/control to fill_parent will force it to fill the entire screen or empty the parent control
wrap_content: The content is big, just can display the content so far
Orientation: arrangement mode, vertical vertical, horizontal level by default
2. Text box:
< TextView Android:id = "@+id/lbl_loginpass" Android:layout_width = "Wrap_content" Android:layout_height = "Wrap_content" Android:text = "@string/lbl_loginpass_text" android:textsize = "15.0SP" />
There are two unknown attributes:
@+id/lbl_loginpass and @string/lbl_loginpass_text
Can actually write directly:
android:text= "User Name"
The system will prompt this is hard coding, recommended rewriting (Khan, wrote for so many years hard-coded), he means that only reference the name of a variable in the dictionary, the specific value in the dictionary to maintain, then where is the dictionary?
This dictionary was maintained in Res/values/string.xml:
<Resources> <stringname= "App_name">Login Demo</string> <stringname= "Action_settings">Settings</string> <stringname= "Lbl_loginname">User name:</string> <stringname= "Lbl_loginpass_text">Password</string> <stringname= "Btn_login">Start landing</string></Resources>
Here the code specification should be noted: if the control ID is lbl_loginpass, then his dictionary name should be lbl_loginpass_text in order to prevent multiple pages of the dictionary name is best to avoid repeating the page prefix, such as Login_lbl_loginpass_ Text
The complete code is as follows:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical"Android:layout_margin= "5dip" > <TextViewAndroid:id= "@+id/form_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Initial user name and password are 123" /> <LinearLayoutAndroid:id= "@+id/layout_login_name"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_margin= "5.0dip"Android:layout_margintop= "10.0dip"android:orientation= "Horizontal" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/lbl_loginname" /> <EditTextAndroid:id= "@+id/txt_login_name"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:textsize= "15.0SP" /> </LinearLayout> <LinearLayoutAndroid:id= "@+id/login_pwd_layout"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_below= "@id/layout_login_name"Android:layout_centerhorizontal= "true"Android:layout_margin= "5.0dip"android:orientation= "Horizontal" > <TextViewAndroid:id= "@+id/login_pass_edit"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/lbl_loginpass"android:textsize= "15.0SP" /> <EditTextAndroid:id= "@+id/txt_login_pwd"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:password= "true"android:textsize= "15.0SP" /> </LinearLayout> <ButtonAndroid:id= "@+id/btn_login"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:gravity= "Center"Android:onclick= "Btn_click"Android:text= "Login" /></LinearLayout>
3. Page background
Open the corresponding background code for the page: Mainactivity.java
To manually implement a button's Click event:
Public voidBtn_click (View v) {TextView Lblinfo=(TextView) Findviewbyid (r.id.form_title); EditText Txt_login_name=(EditText) Findviewbyid (r.id.txt_login_name); EditText Txt_login_pass=(EditText) Findviewbyid (R.ID.TXT_LOGIN_PWD); String LoginName=Txt_login_name.gettext (). toString (). Trim (); String Loginpass=Txt_login_pass.gettext (). toString (). Trim (); if(Loginpass.equals ("123") &&loginname.equals ("123") {Lblinfo.settext ("Login Successful! "); } Else{Lblinfo.settext ("Login failed! "); } }
In fact, these do not say a look at the name to know what the meaning.
. NET Programmer Android Learning Path 1: Login interface