Getting started with Android: Toast, Menu usage and destruction of activities

Last: Getting started with Android (3): Basic usage of activities

Toast

In the previous article onCreate() added some code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);
    Button button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(FirstActivity.this, "You clicked Button1",
                           Toast.LENGTH_SHORT).show();
        }
    });
}

The findViewById method takes the elements defined in the layout file and returns a View object that needs to be transformed downward into a Button object.Once you get an example of a button, we register a listener for the button by calling the setOnClickListener method, so clicking the button executes the onClick method in the listener.

Toast creates a Toast object using the static method makeText(), then calls show() to display the Toast, which requires three parameters to be passed in:

  1. Context: Context, since the activity itself is a Context object, FirstActivity.this is passed in directly here
  2. Text Content
  3. Display duration

Menu

Create a new menu folder in the res directory_a new main Menu resource file in the menu to write the following code:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:id="@+id/add_item"
        android:title="Add"></item>
    <item
        android:id="@+id/remove_item"
        android:title="Remove"></item>
</menu>

Return to activity to override onCreateOptionsMenu method

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

The MenuInflater object is obtained from the getMenuInflater method, and its inflate() method is called to create a menu for the current activity. The second parameter specifies which Menu object our menu item will be added to. The menu parameter passed in the onCreateOptionsMenu method is used directly here.Returns whether the bool value displays the created menu

If you want to implement the logical functionality of menus, you need to override the onOptionsItemSelected method:

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.add_item:
            Toast.makeText(this, "You clicked Add",
                           Toast.LENGTH_SHORT).show();
            break;
        case R.id.remove_item:
            Toast.makeText(this, "You clicked Remove",
                           Toast.LENGTH_SHORT).show();
            break;
        default:
    }
    return true;
}

The result is shown in the figure:

Destroy an activity

You can actually destroy this activity by clicking the return key, but you can also destroy it by code. Let's modify the button's function

 @Override
public void onClick(View v) {
    finish(); // Destruction Activities
}

Summary:

  1. Some controls first get the corresponding object through the id and then override it to interact with the interface
83 original articles published. 12% praised. 10,000 visits+
Private letter follow

Tags: Android xml encoding

Posted on Fri, 14 Feb 2020 20:55:43 -0800 by deathrider