x Add a method with the following signature public void onCreateBundle

X add a method with the following signature public

This preview shows page 3 - 6 out of 8 pages.

x Add a method with the following signature: public void onCreate(Bundle savedInstanceState. public void onCreate(Bundle savedInstanceState) { } This method will be called when the Activity starts and is where initialization of local and member data will be done. Inside this method perform the following: make a call to super.onCreate(savedInstanceState); public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); } This should always be done and is to ensure that any necessary parent class initializations are performed. Also make a call to this.setContentView(R.layout.name_getter); public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .setContentView(R.layout. name_getter ); } When you created the XML layout file earlier, the Android Eclipse Plug-in automatically added a static constant to the static R.layout class in the R.java file under the /gen folder. This constant variable has the same name of the file and it’ s value is used to identify the layout file. This call tells Android to create a screen based off of the layout file. x Make a call to this.findViewById(R.id. <EditText id> ) and set your EditText member variable equal to the return value. <EditText id> should be replaced with the android:id that was specified in the name_getter.xml layout file for the EditText element. You will have to explicitly cast the return value to the type of EditText as this method only returns objects of type Object. This static constant value was added in the same way as it was done for the R.layout.name_getter value and serves the same purpose.
Image of page 3
public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .setContentView(R.layout. name_getter ); name = (EditText) this .findViewById(R.id. editText1 ); } x Make a call to this.findViewById(R.id. <Button id> ) and set a local android.widget.Button reference equal to the return value. <Button id> should be replaced with the android:id that was specified in the name_getter.xml layout file for the Button element. public class GetName extends Activity implements android.view.View.OnClickListener{ android.widget.EditText name ; android.widget.Button button ; public void onClick(View arg0) { // TODO Auto-generated method stub } public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .setContentView(R.layout. name_getter ); name = (EditText) this .findViewById(R.id. editText1 ); button = (Button) this .findViewById(R.id. button1 ); } } x Make a call to button .setOnClickListener(this); button should be replaced with the local Button reference you just retrieved. public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .setContentView(R.layout. name_getter ); name = (EditText) this .findViewById(R.id. editText1 ); button = (Button) this .findViewById(R.id. button1 ); button .setOnClickListener( this ); } }
Image of page 4
x Fill in the onClick method stub: Retrieve the user entered text from the text field and keep it in a local String variable. Create an android.content.Intent object: new Intent(this, Ex1Activity.class).
Image of page 5
Image of page 6

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture