Java Source Code (Cont.)

import android.widget.EditText;
It is a thin veneer over TextView that configures itself to be editable.

import android.support.v7.app.AppCompatActivity;
It is a base class for activities that use the support library action bar features.

import android.view.Menu;
It is the interface for managing the items in a menu.

import android.view.MenuItem;
It is the interface for direct access to a previously created menu item.

public class MainActivity extends AppCompatActivity {
It creates a class definition for the activity we said we wanted (MainActivity). There are two methods almost all subclasses of Activity will implement:

  • onCreate(Bundle), where you initialize your activity, and

  • onPause, where you deal with the user leaving your activity. Any changes made by the user should at this point be committed.

To be of use with Context.startActivity( ), all activity classes must have a corresponding <activity> declaration in their package’s AndroidManifest.

@Override
It is used to mark methods that override a method declaration in a superclass. Compilers produce an error if a method annotated with @Override does not override a method in a superclass.
HelloWorld/app/src/main/java/com/example/wenchen/helloworld/MainActivity.java
package com.example.wenchen.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    final EditText name = (EditText) findViewById( R.id.name );
    final Button button = (Button)   findViewById( R.id.next );
    button.setOnClickListener(
      new View.OnClickListener( ) {
        public void onClick( View v ) {
          /** Here i calls a new screen. **/
          Intent i = new Intent( MainActivity.this, NextActivity.class );
          i.putExtra( "name", name.getText( ).toString( ) );
          startActivity( i );
        }
      }
    );
  }

  @Override
  public boolean onCreateOptionsMenu( Menu menu ) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater( ).inflate( R.menu.menu_main, menu );
    return true;
  }

  @Override
  public boolean onOptionsItemSelected( MenuItem item ) {
    // Handle action bar item clicks here.  The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId( );

    // noinspection SimplifiableIfStatement
    if ( id == R.id.action_settings ) {
      return true;
    }
    else if ( id == R.id.next ) {
      Intent i = new Intent( MainActivity.this, NextActivity.class );
      startActivity( i );
    }
    return super.onOptionsItemSelected( item );
  }
}




      It is unwise to be too sure of one’s own wisdom.    
      It is healthy to be reminded that the strongest might weaken and the wisest might err.    
      ― Mahatma Gandhi