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
01package com.example.wenchen.helloworld;
02 
03import android.app.Activity;
04import android.content.Intent;
05import android.os.Bundle;
06import android.view.View;
07import android.widget.Button;
08import android.widget.EditText;
09import android.view.Menu;
10import android.view.MenuItem;
11 
12public class MainActivity extends Activity {
13  @Override
14  protected void onCreate( Bundle savedInstanceState ) {
15    super.onCreate( savedInstanceState );
16    setContentView( R.layout.activity_main );
17    final EditText name = (EditText) findViewById( R.id.name );
18    final Button button = (Button)   findViewById( R.id.next );
19    button.setOnClickListener(
20      new View.OnClickListener( ) {
21        public void onClick( View v ) {
22          /** Here i calls a new screen. **/
23          Intent i = new Intent( MainActivity.this, NextActivity.class );
24          i.putExtra( "name", name.getText( ).toString( ) );
25          startActivity( i );
26        }
27      }
28    );
29  }
30 
31  @Override
32  public boolean onCreateOptionsMenu( Menu menu ) {
33    // Inflate the menu; this adds items to the action bar if it is present.
34    getMenuInflater( ).inflate( R.menu.menu_main, menu );
35    return true;
36  }
37 
38  @Override
39  public boolean onOptionsItemSelected( MenuItem item ) {
40    // Handle action bar item clicks here.  The action bar will
41    // automatically handle clicks on the Home/Up button, so long
42    // as you specify a parent activity in AndroidManifest.xml.
43    int id = item.getItemId( );
44 
45    // noinspection SimplifiableIfStatement
46    if ( id == R.id.action_settings ) {
47      return true;
48    }
49    else if ( id == R.id.next ) {
50      Intent i = new Intent( MainActivity.this, NextActivity.class );
51      startActivity( i );
52    }
53    return super.onOptionsItemSelected( item );
54  }
55}




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