First, the design of the login page activity_main
First lay out the page, design the basic buttons and boxes that need to be logged in:
After designing, change the corresponding id. The id of the account I set is usertext, the id of the password is passwordtext, and the id of the login button is login. (Note that when designing the id, it is best to add a component to change an id, because the position of the following components needs to use the position of each component. If you change the id after all the design is completed, you need to change all the previous ids, otherwise the layout will be chaos)
Second, the page design of the jump after login
1. Create a new class in the same package of MainActivity, I set it as Home;
code show as below:
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class Home extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.home); } }
2. Create a home xml file in the layout folder of res (note the name is lowercase letters or numbers, otherwise an error will be reported)
3. Configuration declaration (you need to declare the newly created xml file to be able to use it, otherwise the operation will go wrong)
Add the declaration of the newly created home.xml to the AndroidMainfest.xml file at the end of the project, and add the following code after </activity>:
<activity android:name="com.example.test.Home" android:label="@string/app_name" > </activity>
Third, the realization of the login function
In MailActivity, use the click event to realize page jump and account password verification:
method one:
protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) MainActivity.this.findViewById(R.id.login); button.setOnClickListener(login1); }; private View.OnClickListener login1 = new View.OnClickListener(){ public void onClick(View v) { EditText userName = (EditText)MainActivity.this.findViewById(R.id.usertext); //find the id of usertext EditText userPwd = (EditText)MainActivity.this.findViewById(R.id.passwordtext); // Find the id of passwordtext String name = userName.getText().toString(); //Get the input content of the account and assign it to name String pwd = userPwd.getText().toString(); //Get the content of the password input and give it to pwd //Toast toast = Toast.makeText(MainActivity.this, "Account"+name+"\r\nPassword"+pwd, 5); //toast.show(); //The two lines of code commented above can output the content of the account password on the page if(name.contentEquals("lym")&&pwd.contentEquals("12345")){ //Verify whether the entered content account is correct Intent intent=new Intent(); intent.setClass(MainActivity.this, Home.class); MainActivity.this.startActivity(intent); } } };
Method Two:
The above only uses the method of the function to realize the click event, and the method of the inner class can also be used to realize the page jump.
public class MainActivity extends Activity { private Button login; private EditText user; private EditText password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) MainActivity.this.findViewById(R.id.login); //button.setOnClickListener(login1); login=(Button)findViewById(R.id.login); user = (EditText)MainActivity.this.findViewById(R.id.usertext); //find the id of usertext password = (EditText)MainActivity.this.findViewById(R.id.passwordtext); //Find the id of passwordtext login.setOnClickListener(new OnClickListener(){ public void onClick(View v) { user = (EditText)MainActivity.this.findViewById(R.id.usertext); //find the id of usertext password = (EditText)MainActivity.this.findViewById(R.id.passwordtext); //Find the id of passwordtext String name = user.getText().toString(); //Get the input content of the account and assign it to name String pwd = password.getText().toString();//Get the input content of the password and give it to pwd if(name.contentEquals("lym")&&pwd.contentEquals("12345")){ //Verify whether the entered content account is correct Intent intent=new Intent(); intent.setClass(MainActivity.this, Home.class); MainActivity.this.startActivity(intent); } } }); };
Although the second method has less code, it is more troublesome to handle multiple click events. The code is all put together, which is messy. The first method is defined outside the class implementation function, which is easier to view and manage the code.
Finally, the login function is implemented: