Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Treehouse: User input from Text
- public class MainActivity extends ActionBarActivity {
- private EditText mNameField;
- private Button mStartButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mNameField = (EditText)findViewById(R.id.nameEditText);
- mStartButton = (Button)findViewById(R.id.startButton);
- mStartButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String name = mNameField.getText().toString();
- startStory(name);
- }
- });
- }
- private void startStory(String name){
- Intent intent = new Intent(this, StoryActivity.class);
- intent.putExtra(getString(R.string.key_name), name);
- startActivity(intent);
- }
- }
- //--------------------------------------------------------------------------------------------------------------------------------------
- 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
- 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.
- private Button mStartButton;
- @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.
- 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.
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mNameField = (EditText)findViewById(R.id.nameEditText); // Blank ____ to input name in app
- mStartButton = (Button)findViewById(R.id.startButton); // Button to submit name
- mStartButton.setOnClickListener(new View.OnClickListener() { //onClickListener for the button
- @Override
- public void onClick(View v) {
- String name = mNameField.getText().toString(); // String to hold name //".getText" to get the text from the field // ".toString" to convert it to a String
- startStory(name);
- }
- });
- }
- private void startStory(String name){
- Intent intent = new Intent(this, StoryActivity.class);
- intent.putExtra(getString(R.string.key_name), name);
- startActivity(intent);
- }
- }
- // 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