Java Source Code (Cont.)

final EditText name = (EditText) findViewById( R.id.name );
A final class cannot be extended for reasons of security and efficiency. EditText is a subclass of TextView supplied with text editing operations. It is often used in the applications in order to provide an input or text field, especially in forms.

final Button button = (Button) findViewById( R.id.next );
The method findViewById of the class View looks for a child view with the given id such as R.id.next. If this view has the given id, return this view. The id next is defined in the file activity_main as
   <Button android:id     = "@+id/next"
    android:layout_width  = "wrap_content"
    android:layout_height = "wrap_content"
    android:text          = "@string/next_page" />
where the text of next_page is “Next Page” defined in strings.

button.setOnClickListener( View.OnClickListener | )
It is a method of the class View and registers a callback to be invoked when this view, a button in this case, is clicked. If it is not clickable, it becomes clickable.

new View.OnClickListener( )
It is an interface definition for a callback to be invoked when a view is clicked

public void onClick( View v ) {
It is a method of the class View.OnClickListener and is called when a view has been clicked
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 );
  }
}




      Let me just put my face on, and I’ll meet you at the restaurant in 15 minutes.