The login interface is a classic and basic program for graphic programming and web programming.
In Android, a basic login interface:
Click the Cancel button to close the program, click the login button to display the user entered the user name and password.
First, the basic layout
This app takes advantage of the table layout in Android.
First, a few strings are defined in Res/values/strings.xml. The reason why the string is not directly written directly in the Activity_main.xml component is because it is not possible to warn of eclipse. The code for this file is as follows:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" > Login Interface </string > <string name= "action_settings" >Settings</string> <string name= "Textview1" > User name < /string> <string name= "textview2" > Password </string> <string name= "button1" > Login </string > <string name= "Button2" > Cancel </string></resources>
After that, all code is deleted in Res/layout/activity_main.xml, which is the HelloWorld label text of the TextView component that is in the relative layout that comes with the new project.
Write our login box with a table layout.
Define a table layout tablelayout, the entire table layout width, height are full screen, occupy the entire main_activity. This has already been said in "Android" with Java Code Layout, button add Click event (click to open link).
After that, setting its Gravity property can only be centered vertically, and the tablelayout will not be centered horizontally. The real horizontal center is the android:gravity= "Center_horizontal" in the TableRow component of each row.
This table component is actually very similar to the table tag in HTML, table-tr. It just doesn't have TD.
You define the number of components in a tablerow, and it gives you as many TD.
<tablelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:gravity= "center" > <!--First line--<tablerow Android : layout_width= "match_parent" android:layout_height= "wrap_content" android:gravity= "Center_horizontal" > <textview android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "@string/textview1" android:textsize= "24sp"/> <edittext android:id= "@+ Id/edittext1 "android:layout_width=" 200DP "android:layout_height=" Wrap_content "android:i Nputtype= "text" android:textsize= "24sp"/> </TableRow> <!--second line--<tablerow Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "center_horizontal "> <textview Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "@str Ing/textview2 "android:textsize=" 24sp "/> <edittext android:id=" @+id/edittext2 " Android:layout_width= "200DP" android:layout_height= "wrap_content" android:inputtype= "Textpasswor D "android:textsize=" 24sp "/> </TableRow> <!--third line--<tablerow Android:layo Ut_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "Center_horizontal" > <button android:id= "@+id/button1" android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:text= "@string/button1"/> <button android:id= "@+id/button 2 "android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:text= "@string/button2"/> </TableRow></TableLayout>
In the first row now a TextView label text, width height is wrapped text is OK, inside the text size is 24sp. sp,scaled pixels, enlarge pixels. Mainly used for font display. According to Google's suggestion, TextView's font size is best to use SP to do units
It is then an edit box with a fixed width of 200dp. This will make the cells in the table layout larger. Lest the input box is too small, so that the user is too sore. Dp,device independent pixels, device independent pixels, different devices have different display effect, this and device hardware, generally we support WVGA, HVGA and QVGA recommend this, do not rely on pixels. Then this edit box input method is a text box, give a id=edittext1 to him, a moment to go to the Java file to get its input value, this is basically with JavaScript is a reason.
There is no difference between the second line and the first line, except that the ID of the edit box becomes edittext2, and the input method is not the same as the password box.
Here the static label text TextView because we do not control it in the Java file for a moment because it is not set to the ID.
The last line is two buttons, set the corresponding ID. Create events in the Java file for them in a moment.
Second, Mainactivity.java
The next is to create a click event for two buttons. In Mainactivity.java code, like STRUTS2, you first declare the variables you want to manipulate in the entire method, that is, buttons 1, 2, edit box 1, 2, and of course those annoying getter and setter are not needed. This is like the phrase document.getElementById ("Component ID") in JavaScript, and Android is Findviewbyid (r.id. Component ID) through (coercion type conversion). Let the components you set up in XML relate to the Java files.
Package Com.example.xml_java_view;import Android.os.bundle;import Android.app.activity;import android.view.Menu; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends Activity {private Button button1 ;p rivate Button button2;private EditText edittext1;private EditText edittext2; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) Findviewbyid (r.id.button1); button2 = (Button) Findviewbyid (r.id.button2); edittext1 = (EditText) Findviewbyid ( R.ID.EDITTEXT1); edittext2 = (EditText) Findviewbyid (R.ID.EDITTEXT2); Button1.setonclicklistener (New Onclicklistener () {//For Button1 add click event @overridepublic void OnClick (View v) {toast.maketext (Mainactivity.this, "username:" + edittext1.gettext () + ", Password:" + edittext2.gettext (), Toast.length_long). Show ();//Fade out Message Prompt}}); Button2.setonclickliStener (New Onclicklistener () {//For Button2 Add click event @overridepublic void OnClick (View v) {system.exit (0);//Exit Program}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}Click on the "Login" button, the Toast component is displayed, which is the message prompt component that will disappear automatically after the display.
The method shown above gives the toast component the second parameter in the key Maketext, setting the displayed text.
After passing (forcing type conversion) Findviewbyid (r.id. Component ID) to get the text of the EDITTEXT1 and edittext2 two components set in the XML file, the direct reality comes out.
As for the Cancel button, the Exit program event is system.exit (0) in Java.
At this point, the entire login screen app is done.
"Android" using table layout, Android XML file with Java Interactive Production login interface