Android Example: a simple login program

Source: Internet
Author: User

Recently wrote a simple landing procedure, there are a few gains:

1. Know how to nest linearlayout in LinearLayout, perfect the ranks of the layout;

2. Use Android:layout_weight to control the weight of the control;

3. Use GetText () to obtain edittext content;

4. Familiar with the writing of the control, no longer follow the book copy =.=

The code is as follows:

Loginactivity.java

ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classLoginactivityextendsActivity {Privatebutton button; PrivateEditText EditText1; PrivateEditText editText2; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.login); //loading controls such as buttons and file boxesbutton=(Button) Findviewbyid (R.id.button); EditText1=(EditText) Findviewbyid (R.id.input_account); EditText2=(EditText) Findviewbyid (R.id.input_password); Button.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {//get the text content of the EditText and delete the spaceString inputaccount=Edittext1.gettext (). toString (). Trim (); String Inputpassword=Edittext2.gettext (). toString (). Trim (); //If the account password is "123", jump to the activity                    if(Inputaccount.equals ("123") && Inputpassword.equals ("123") {toast.maketext (loginactivity). This, "The account password is correct, is logged in", Toast.length_short). Show (); Intent Intent=NewIntent (loginactivity. This, Mainactivity.class);                    StartActivity (Intent); }Else{toast.maketext (loginactivity). This, "The account or password is not correct, please re-enter", Toast.length_short). Show ();            }            }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (r.menu.setting, menu); return true; }    }
Loginactivity.java

Mainactivity.java

ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (r.menu.setting, menu); return true; }    }
Mainactivity.java

Login.xml

<LinearLayoutxmlns: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=". Loginactivity "android:orientation= "vertical"    >        <LinearLayoutandroid:orientation= "Horizontal"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"            >                        <TextViewAndroid:id= "@+id/account"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "account Number:"    />           <EditTextAndroid:id= "@+id/input_account"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:hint= "Input your account"Android:layout_weight= "1" >                        </EditText>             </LinearLayout>              <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal">           <TextViewAndroid:id= "@+id/password"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Password:"        />        <EditTextAndroid:id= "@+id/input_password"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:hint= "Input Your password"Android:layout_weight= "1"      />      </LinearLayout>        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal">            <ButtonAndroid:id= "@+id/button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Login" />                    </LinearLayout></LinearLayout>
Login.xml

Activity_main.xml

<Relativelayoutxmlns: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 " >    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Successful landing!" Sprinkle flower ~ " /></Relativelayout>
Activity_main.xml

Registering activity in Androidmanifest.xml

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.logindemo"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.example.logindemo.LoginActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>                <ActivityAndroid:name= "Com.example.logindemo.MainActivity" >         </Activity>    </Application></Manifest>
Androidmanifest.xml

The results are as follows:

The account password is entered "123", you can successfully login. Happy ^_^

Android Example: a simple login program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.