Study Notes - Week 3

This week's Android learning summary

activity life cycle

test:
Write the following code in MainActivity.java

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "life_cycle";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate invoked. (create)");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart invoked. (started)");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume invoked. (restored)");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause invoked. (pause)");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop invoked. (stopped)");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart invoked. (restarted)");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy invoked. (release resources)");
    }
}

Click the fourth button logcat in the lower left corner, and select Edit Filter Configuration in the drop-down box on the far right

Select filter by tag and write tag name, select debug below


The life cycle is not fully understood. It is also well understood that users can interact during resume and suspend, but I always feel that something is missing; I have seen many applications in this area, such as a machete shopping software, you There are always a bunch of pop-up windows popping up when you quit after helping others hack a knife. This should be set when you call the callback methods to close the application (personal understanding).

Window jump (carry data)

First write the home page, pay attention to adding the id attribute to the place where the data needs to be carried and the jump button

<?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:orientation="vertical"
    android:gravity="center"
    android:padding="15sp"
    android:background="@drawable/bk012"
    tools:context=".LoginActivity">

    <TextView
        android:id="@+id/tvUserLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user_login"
        android:textColor="#ff00ff"
        android:textSize="25sp"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_marginBottom="30dp">

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

        <EditText
            android:id="@+id/edtUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/input_username"
            android:ems="10"
            android:singleLine="true"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

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

        <EditText
            android:id="@+id/edtPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/input_password"
            android:inputType="textPassword"
            android:ems="10"
            android:singleLine="true"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:background="@color/danlv"
            android:textColor="#ffffff"
            android:text="@string/login"
            android:layout_marginRight="10dp"
            android:textSize="20sp"/>

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

Then write the corresponding Java page (the comments are very clear)

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);
        //Activity bar icons
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setLogo(R.mipmap.ic_launcher);
        //Get control instance
        edtUsername = findViewById(R.id.edtUsername);
        edtPassword = findViewById(R.id.edtPassword);
        btnLogin = findViewById(R.id.btnLogin);
        btnCancel = findViewById(R.id.btnCancel);
        //Register a listener for the login button
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Save user input data
                String strUsername=edtUsername.getText().toString().trim();//trim() removes spaces entered by the user in front
                String strPassword=edtPassword.getText().toString().trim();
                //Check if the username and password are correct
                if (strUsername.equals("admin") && strPassword.equals("pwd")) {
                    //Use toast to prompt users to log in
                    Toast.makeText(LoginActivity.this, "login successful", Toast.LENGTH_SHORT).show();//LENGTH_SHORT indicates the length of time
                    //Create display intent
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    //Create packet encapsulation data (encapsulation)
                    Bundle date = new Bundle();
                    date.putString("username", strUsername);
                    date.putString("password", strPassword);
                    //Carry packets by intent
                    intent.putExtras(date);
                    //Start the target component as intended
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "username or password is wrong", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //close the window
                finish();
            }
        });
    }
}

Then write the page after the jump (just a simple display here), or pay attention to adding the id attribute

<?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"
    android:background="@drawable/bk016"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="25dp"
        android:textColor="#ff00ff"/>

</LinearLayout>

Then write the Java file corresponding to the page after the jump to receive data

public class MainActivity extends AppCompatActivity {
    protected TextView tvMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Set up the user interface with layout resources
        setContentView(R.layout.activity_main);
       //Get control instance by resource identifier
        tvMessage = findViewById(R.id.tvMessage);
        //get intent
        Intent intent = getIntent();
        //Determine if the intent is empty
        if (intent != null) {
            //Get the intent to carry the packet (encapsulation)
            Bundle date = intent.getExtras();
            String username = date.getString("username");
            String password = date.getString("password");
            //Splicing user information
            String message = "Landed successfully!\n user:" + username + "\n password:" + password;
            //Set label properties to display user information
            tvMessage.setText(message);
        }

    }
}

Finally add the layout file to start

Effect


Jumping this part is really interesting to learn (perhaps because the visualization can be changed), this part is not difficult and mainly cumbersome; I only talked about encapsulation, mainly because I have a kind of thinking (maybe most people have ) – data encapsulation is more professional and secure. The logic of encapsulation is also very clear.

Tags: Java Android Design Pattern

Posted by gamerzfuse on Sat, 14 May 2022 09:20:05 +0300