Advertisement
yo2man

Treehouse: User input from Text

Jun 29th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. //Treehouse: User input from Text
  2.  
  3. public class MainActivity extends ActionBarActivity {
  4.  
  5.     private EditText mNameField;
  6.     private Button mStartButton;
  7.  
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.  
  13.         mNameField = (EditText)findViewById(R.id.nameEditText);
  14.         mStartButton = (Button)findViewById(R.id.startButton);
  15.  
  16.         mStartButton.setOnClickListener(new View.OnClickListener() {
  17.             @Override
  18.             public void onClick(View v) {
  19.                 String name = mNameField.getText().toString();
  20.                 startStory(name);
  21.             }
  22.         });
  23.     }
  24.  
  25.     private void startStory(String name){
  26.         Intent intent = new Intent(this, StoryActivity.class);
  27.         intent.putExtra(getString(R.string.key_name), name);
  28.         startActivity(intent);
  29.     }
  30. }
  31.  
  32. //--------------------------------------------------------------------------------------------------------------------------------------
  33. public class MainActivity extends Activity { //MainActivity extends Activity meaning Main activity class is a subclass/child class of Activity class and inherits all the stuff from Activity
  34.  
  35.     private EditText mNameField; // Remember that this private keyword means that this field can only be used within this class. Which is what we want. We will generally make member variables private like this.
  36.  
  37.     private Button mStartButton;
  38.  
  39.     @Override  // @Override - Java annotations are hints to the compiler that help us while developing. They aren't actually required for the code to work. This hint states that this method is overriding the onCreate method in the parent class.
  40.  
  41.     protected void onCreate(Bundle savedInstanceState) { // why is onCreate here protected instead of private? Protected is kind of a cross between public and private. It makes things available to other classes in the same package or sub-classes of this class.
  42.  
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.activity_main);
  45.  
  46.         mNameField = (EditText)findViewById(R.id.nameEditText); // Blank ____ to input name in app
  47.         mStartButton = (Button)findViewById(R.id.startButton);  // Button to submit name
  48.  
  49.         mStartButton.setOnClickListener(new View.OnClickListener() { //onClickListener for the button
  50.             @Override
  51.             public void onClick(View v) {
  52.                 String name = mNameField.getText().toString(); // String to hold name //".getText" to get the text from the field // ".toString" to convert it to a String
  53.                 startStory(name);
  54.             }
  55.         });
  56.     }
  57.  
  58.     private void startStory(String name){
  59.         Intent intent = new Intent(this, StoryActivity.class);
  60.         intent.putExtra(getString(R.string.key_name), name);
  61.         startActivity(intent);
  62.     }
  63. }
  64.  
  65.  
  66. // https://teamtreehouse.com/library/build-an-interactive-story-app/user-input/getting-text-from-an-edittext
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement