Getting Started with AndroidStudio - Creating a Landing Page

  I am a beginner in android. Originally, the laboratory used Eclipse for android development, but there were some bugs in the process of configuring the environment, and I could not find R.java. , Abandoned the pit and chose AndroidStudio (I used it a little before), and then do follow-up updates after eclipse is solved. Next is some introduction about getting started with AndroidStudio;

   Create a new Project (select empty Activity)

 1. Directory analysis

   Each activity corresponds to a page, JAVA stores all our Java code, a Java file corresponds to an Activity,

   Every time you create an Activity, you have to go to AndroidManifest.xml to register (must!!! Otherwise, the jump will go wrong),

   The res file stores the pictures, strings, layouts and other resources we use in the project. In this example, only the layout layout is involved; we can mainly use these three places

 

AndroidManifest.xml registers login and index

2. Login interface

(1) Page design - activity_index

 

You can select some commonly used button controls in the design menu bar, drag and drop to generate a simple interface, which is relatively simple, convenient, and easy to understand. It is very friendly to Xiaobai. The generated XML file is as follows:

<TextView
        android:text="username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="49dp"
        android:id="@+id/user" />

    <EditText
        android:hint="Please enter a username"//Prompt the user to enter a username
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="21dp"
        android:layout_marginEnd="21dp"
        android:layout_marginTop="36dp"
        android:id="@+id/username" />

    <TextView
        android:text="password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/username"
        android:layout_alignLeft="@+id/user"
        android:layout_alignStart="@+id/user"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="75dp"
        android:id="@+id/pwd" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_alignBottom="@+id/pwd"
        android:layout_alignRight="@+id/username"
        android:layout_alignEnd="@+id/username"
        android:id="@+id/password" />

    <Button
        android:text="login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_alignRight="@+id/password"
        android:layout_alignEnd="@+id/password"
        android:layout_marginRight="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginTop="79dp"
        android:id="@+id/login" />
</RelativeLayout>

(2)login.java
login.java is mainly to implement the function of the login interface.
public class login extends AppCompatActivity {
    private Button login;
    EditText username;
    EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_login);
        login=(Button)findViewById(R.id.login);
        username=(EditText)super.findViewById(R.id.username);//Get the username entered by the user
        password=(EditText)super.findViewById(R.id.password);//Get user password
        login.setOnClickListener(new View.OnClickListener()//Listen to the login click event
        {
            public void onClick(View v)
            {//Verify whether the username and password meet the requirements
                if(username.getText().toString().equals("admin")&&password.getText().toString().equals("888888"))
                {
                    Toast.makeText(getApplicationContext(), "Login successful", Toast.LENGTH_SHORT).show();//Prompt the user to log in successfully
                    Intent t=new Intent(login.this,index.class);//Jump from the login page to the index interface
                     startActivity(t);
                }
                if(!username.getText().toString().equals("admin")||!password.getText().toString().equals("888888"))
                {
                    Toast.makeText(getApplicationContext(), "Incorrect username or password", Toast.LENGTH_SHORT).show();//Prompt user username or password is incorrect
                }
            }
        }
        );
    }
}

3. Interface effect

 

 Clicking on login will show that the login is successful and jump to the home page (the home page means that you just write a welcome login, so you won’t be embarrassed to take it out)

 There is always want to test the effect on the mobile phone, but it keeps showing that the device cannot be found, so... continue to solve it, and update it when it is solved.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326159722&siteId=291194637