/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.explicitintent; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { /* Fields that will store our EditText and Button */ private EditText mNameEntry; private Button mDoSomethingCoolButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * Using findViewById, we get a reference to our Button from xml. This allows us to * do things like set the onClickListener which determines what happens when the button * is clicked. */ mDoSomethingCoolButton = (Button) findViewById(R.id.b_do_something_cool); mNameEntry = (EditText) findViewById(R.id.et_text_entry); /* Setting an OnClickListener allows us to do something when this button is clicked. */ mDoSomethingCoolButton.setOnClickListener(new OnClickListener() { /** * The onClick method is triggered when this button (mDoSomethingCoolButton) is clicked. * * @param v The view that is clicked. In this case, it's mDoSomethingCoolButton. */ @Override public void onClick(View v) { /* * Storing the Context in a variable in this case is redundant since we could have * just used "this" or "MainActivity.this" in the method call below. However, we * wanted to demonstrate what parameter we were using "MainActivity.this" for as * clear as possible. */ Context context = MainActivity.this; String message = "Button clicked!\nTODO: Start a new Activity and pass some data."; Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } }); } }