Android learning records (7)


After a few days of systematic study, the instructor gave us several new tasks that we needed to complete within the specified time. Today my learning content is to complete the first task-Activity page jump.

One.Activity

The activity represents a single screen with a user interface, such as a Java window or frame. Android activities are subclasses of the ContextThemeWrapper class.
The callback defined by Activity is shown in the following table

Callback description
onCreate() This is the first callback, called when the activity is first created
onStart() This callback is called when the activity is visible to the user
onResume () This callback is called when the application and the user start to interact
onPause() The suspended activity cannot accept user input and cannot execute any code. Called when the current activity is about to be suspended and the previous activity is about to be resumed
onStop () Called when the activity is no longer visible
onDestroy () Called before the activity is destroyed by the system
onRestart() Called when the activity is stopped and then reopened

2. Case-Realizing the user login interface

This task requires us to meet the following requirements:
(1) Declare button control variables btnLogin, btnCancel in the login interface class
(2) Get the instance of the two buttons through the findViewById method
(3) Use the setOnClickListener method to register the two buttons Click event listener
(4) to implement the click event listener interface, using anonymous implementation
(5) write event handling code in the abstract method of the interface

Implementation steps

(1) Create a new project-UserLogin

Insert picture description here
Insert picture description here

(2) Add a background image

Add your favorite background image to the new project
Insert picture description here

(3) Create a login window-LoginActivity

Insert picture description here
Insert picture description here

Insert picture description here

(4) Write the file-activity_login.xml

Insert picture description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center"
    android:padding="15dp"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvUserLogin"
        android:layout_marginBottom="30dp"
        android:text="@string/user_login"
        android:textColor="#ff00ff"
        android:textSize="25sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvUsername"
            android:text="@string/username"
            android:textColor="#000000"
            android:textSize="20sp"/>
        <EditText
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:id="@+id/edtUsername"
            android:ems="10"
            android:hint="@string/input_username"
            android:singleLine="true"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvPassword"
            android:text="@string/password"
            android:textColor="#000000"
            android:textSize="20sp"/>

        <EditText
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_password"
            android:id="@+id/edtPassword"
            android:inputType="textPassword"
            android:singleLine="true"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnLogin"
            android:paddingRight="30dp"
            android:paddingLeft="30dp"
            android:text="@string/login"
            android:textSize="20sp"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnCancel"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="@string/cancel"
            android:textSize="20sp"/>
    </LinearLayout>

</LinearLayout>

(5) Write the file-activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvMessage"
        android:textSize="25dp"
        android:textColor="#0000ff"/>

</LinearLayout>

(6) Modify the project list file

Insert picture description here

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.nell.userlogin">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.UserLogin">
        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <!--<intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>-->
        </activity>
    </application>

</manifest>

(7) Modify the string resource file

Insert picture description here

<resources>
    <string name="app_name">UserLogin</string>
    <string name="user_login">用户登录</string>
    <string name="username">用户:</string>
    <string name="input_username">请输入用户名</string>
    <string name="password">密码:</string>
    <string name="input_password">请输入密码</string>
    <string name="login">登录</string>
    <string name="cancel">取消</string>
</resources>

(8) Writing the login window-LoginActivity

Insert picture description here

package net.nell.userlogin;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    
    
    private EditText edtUsername;
    private EditText edtPassword;
    private Button btnLogin;
    private Button btnCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        edtUsername = findViewById(R.id.edtUsername);
        edtPassword  = findViewById(R.id.edtPassword);
        btnCancel = findViewById(R.id.btnCancel);
        btnLogin = findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(new View.OnClickListener(){
    
    

            @Override
            public void onClick(View v) {
    
    
                String strUsername = edtUsername.getText().toString().trim();
                String strPassword = edtPassword.getText().toString().trim();

                if (strUsername.equals("admin") && strPassword.equals("admin")){
    
    
                    Toast.makeText(LoginActivity.this,"恭喜,用户名与密码正确!",Toast.LENGTH_SHORT).show();;
                }else {
    
    
                    Toast.makeText(LoginActivity.this,"遗憾,用户名或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                finish();
            }
        });
    }
}

(9) Start the project and view the results

1. The password and username are entered correctly

Insert picture description here

2. The username or password is incorrect

Insert picture description here

3. Use intent to start the component

1. Use display intent

The explanation of this method requires the assumption that there are two windows, namely: FirstActivity and SecondActivity

method one:

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);

Method Two:

Intent intent = new Intent();
intent.setClass(FirstActivity.this,SecondActivity.class);
startActivity(intent);

Method three:

Intent intent = new Intent();
ComponentName componen = new ComponentName(FirstActivity.this,SecondActivity.class);
intent.setComponent(component);
startActivity(intent);

2. Use implicit intent

Method 1: Create in java code

Intent intent = new Intent();
intent.setAction("net.nell.ACTION_NEXT");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);

Method 2: Set up intent filters in the project manifest file

<activity android:name="net.nell.SecondActivity">
	<intent-filter>
		<action android:name="net.nell.ACTION_NEXT"/>
		<category android:name="android.intent.category.DEFAULT"/>
	</intent-filter>
</activity>

4. Modify the user login program

Modify the user login program, after entering the correct password and user name, jump to the main window, and display the user name and password in the main window

Implementation

1. Modify LoginActivity

Create the display intent mentioned above on the original basis, use the intent to carry data, and start the target component according to the intent
Insert picture description here

2. Modify MainActivity

package net.nell.userlogin;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    
    
    private TextView tvMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvMessage = findViewById(R.id.tvMessage);

        //获取意图
        Intent intent = getIntent();
        //判断意图是否为空
        if (intent != null){
    
    
            //获取意图携带数据
            String username = intent.getStringExtra("username");
            String password = intent.getStringExtra("password");
            //拼接用户信息
            String message = "登录成功!\n用户:"+ username +"\n密码"+password;
            //设置标签属性,显示用户信息
            tvMessage.setText(message);
        }
        }
    }
}

3. Run the program and view the results

1. The user name and password are correct

Insert picture description here

2. Incorrect username or password

Insert picture description here
Today's learning task is probably the above content. The reason why so much content can be completed in one day is because I have already conducted training on this kind of project in the previous learning content. Tomorrow’s study, I believe it will also make me even further.

Guess you like

Origin blog.csdn.net/weixin_46705517/article/details/112632321