Destruction Toast, Menu use and activities: (4) Android Starter

Previous: Android Getting Started (3): Activities of the basic usage

Toast

onCreate in the last article () to add 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 method of obtaining the element findViewById defined layout file, returns a View object needs to turn it into a downward transition Button object. After obtaining button example, we register a listener for the button by calling setOnClickListener methods, so click on the button will perform the onClick method listeners.

Toast created by static method makeText () a Toast object, and then call the show () Toast will be displayed, which requires passing three parameters:

  1. Context: context, because the activity itself is a Context object, so here passed directly FirstActivity.this
  2. Text content
  3. Shows you how long

Menu

New menu in res directory folder → write the following code in the new main menu in the Menu resource file:

<?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>

Back to rewrite activities onCreateOptionsMenu method

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

GetMenuInflater obtained by the method MenuInflater object, then call it inflate () method to create the currently active menu, the second argument is used to specify our menu items will be added where a Menu object, use the direct method in this incoming onCreateOptionsMenu the menu parameters. Bool return value is created menu display

If you want to implement logic functions menu, then the need to rewrite 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 results are shown below:
Here Insert Picture Description

Destroy an activity

In fact, only need can be destroyed by clicking the return key activity, but can also be destroyed by the code, we modify the function button

 @Override
public void onClick(View v) {
    finish(); // 销毁活动
}

to sum up:

  1. Some controls are to obtain the corresponding object is achieved by id and then rewritten by the interactive interface method
Published 83 original articles · won praise 12 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42317011/article/details/104325520