Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.fourcorners;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log; //for statements like Log.e(...)
- import android.view.Gravity;
- import android.view.View; //for the View datatype
- import android.widget.Button; //here via ALT+ENTER
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- //ALT+ENTER > "import class" > import android.widget.Button;
- //Class data members
- Button mBtnULC=null, mBtnURC=null, mBtnLLC=null, mBtnLRC=null; //cascade declaration
- Context mContext;
- public static final String ERROR_STAMP = "NULL OBJECT";
- View.OnClickListener mClickHandler = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- boolean bCautious = v instanceof Button;
- if (bCautious){
- Button b = (Button)v;
- String strTheButtonText = b.getText().toString();
- Toast t = Toast.makeText(
- mContext,
- //"Button "+strTheButtonText+" was clicked!",
- strTheButtonText +
- " "+
- getString(R.string.strButtonResponse),
- Toast.LENGTH_LONG
- );
- t.setGravity(
- Gravity.CENTER,
- 0,
- 0
- );
- t.show();
- }//if bCautious
- }//onClick
- }; //mClickHandler
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //setContentView(R.layout.activity_main);
- //CTRL+SPACE - all available autocomplete options
- setContentView(R.layout.rl_four_corners_v2);
- //setContentView(R.layout.cl_four_corners);
- init();
- }//onCreate
- public void someMethodInTheActivityClass (View pV){
- Toast t = Toast.makeText(
- this,
- //"hello",
- ((Button)pV).getText().toString(),
- Toast.LENGTH_LONG
- );
- t.show();
- }//someMethodInTheActivityClass
- void init(){
- this.mContext = this;
- //1 - bindings between XML elements and class data members
- //get rid of the null status
- mBtnULC = findViewById(R.id.idBtnULC);
- mBtnURC = findViewById(R.id.idBtnURC);
- mBtnLLC = findViewById(R.id.idBtnLLC);
- mBtnLRC = findViewById(R.id.idBtnLRC);
- //quality control
- Button[] aRelevantButtons = {
- mBtnLLC, mBtnLRC, mBtnURC, mBtnULC
- };
- for (Button b : aRelevantButtons){
- boolean bNull = b==null;
- if (bNull){
- Log.e(
- ERROR_STAMP,
- "Button found null. Aborting."
- );
- finish(); //terminates the Activity
- }//if
- }//for
- //2 - via the Java bindings, assign behavior to the XML elements
- mBtnULC.setOnClickListener(this); //useless
- mBtnURC.setOnClickListener(this); //useless
- mBtnLLC.setOnClickListener(mClickHandler); //useless
- makeAllButtonRespondToClickViaSameHandler(
- aRelevantButtons,
- mClickHandler //Buttons will respond via Class's mClickHandler
- //this //Buttons will respond via Class's onclick
- );
- }//init
- void makeAllButtonRespondToClickViaSameHandler(
- Button[] pButtonsThatShouldBehaveViaTheSameHandler,
- View.OnClickListener pHandler
- ){
- for (Button b : pButtonsThatShouldBehaveViaTheSameHandler){
- b.setOnClickListener(pHandler);
- }//for
- }//makeAllButtonRespondToClickViaSameHandler
- /*
- the contractual interface View.OnClickListener
- comes with a single obligation
- to write the onClick method
- The class itself now provides a solution named "onClick"
- for whatever View object to respond to a click
- */
- @Override
- public void onClick(View v) {
- int iWhichViewWasClicked = v.getId();
- switch(iWhichViewWasClicked){
- case R.id.idBtnULC:
- case R.id.idBtnURC:
- case R.id.idBtnLLC:
- case R.id.idBtnLRC:
- Toast t = Toast.makeText(
- mContext,//Context
- "via implements interface",//String (the message)
- Toast.LENGTH_LONG //int (the message's duration)
- );
- t.show();
- }//switch
- }//onClick
- }//MainActivity
Add Comment
Please, Sign In to add comment