Android Combat (i) Learn multiple controls to implement login and remember password function

Source: Internet
Author: User

First determine the desired control:

Two edittext: For entering account and password

A button: used to log in to see if the account and password are correct

A checkbox: For remembering passwords and accounts

A androidstudio: Used to write code, of course, people recommend the use of Notepad to write code, nonsense does not say much to start.

Create an app project add two layouts two parts java.class, register the second layout inside the androidmanifest.xml.

Ready to finish.

1. Add the above control to the initial layout and set the ID for it.

The code looks like this

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/activity_main"
android:orientation= "Vertical"
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= "Com.example.administrator.app.MainActivity" >

<edittext
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:hint= "User Name"
Android:id= "@+id/edittext"
/>

<edittext
Android:password= "true"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:hint= "Password"
Android:id= "@+id/password"
/>

<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"

android:text= "Login"
Android:id= "@+id/button"/>

<checkbox
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Remember Password"
Android:id= "@+id/remember_password"
/>

</LinearLayout>


2. The Java file code that modifies the corresponding layout file is shown below

public class Mainactivity extends Appcompatactivity {
Private Sharedpreferences pref;
Private Sharedpreferences.editor Editor;
Private EditText Accountedit;
Private EditText Passwordedit;
Private Button login;
Private CheckBox Rememberpass;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Pref = Preferencemanager.getdefaultsharedpreferences (this);
Accountedit = (EditText) Findviewbyid (R.id.edittext);
Passwordedit = (EditText) Findviewbyid (R.id.password);
Rememberpass = (CheckBox) Findviewbyid (R.id.remember_password);
Login = (Button) Findviewbyid (R.id.button);

Boolean isremember = Pref.getboolean ("Remember_password", false);
if (Isremember) {
To set text to a text box
String account = pref.getstring ("Account", "");
String Password = pref.getstring ("Password", "" ");
Accountedit.settext (account);
Passwordedit.settext (password);
Rememberpass.setchecked (TRUE);}
Login.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
String account = Accountedit.gettext (). toString ();
String password = Passwordedit.gettext (). toString ();
if (Account.equals ("1489259653") && password.equals ("123456")) {
Editor = Pref.edit ();
if (rememberpass.ischecked ()) {
Editor.putboolean ("Remember_password", true);
Editor.putstring ("account", account);
Editor.putstring ("password", password);


} else {editor.clear ();}
Editor.commit ();
Intent w = new Intent (mainactivity.this,second.class);
StartActivity (w);
Finish ();
}else{
Toast.maketext (Mainactivity.this, "Incorrect account or password", Toast.length_short). Show ();
}
}
});



}
You can start running.
Each of these controls can be understood, do not know how to contact me QQ1489259653
The code is best to knock it over again, can't copy and paste, only really hit the code, not my stingy but to let you deepen the memory, OK.
If you think it's helpful to you please top it,,,,
If there is a similar calculation I copied,,,,
This is the time to get here.
Forwarding please indicate the source .....



Android Combat (i) Learn multiple controls to implement login and remember password function

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.