am_dot_com

Untitled

Oct 20th, 2021 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. package com.joythis.android.fourcorners;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.util.Log; //for statements like Log.e(...)
  8. import android.view.Gravity;
  9. import android.view.View; //for the View datatype
  10. import android.widget.Button; //here via ALT+ENTER
  11. import android.widget.Toast;
  12.  
  13. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  14.     //ALT+ENTER > "import class" > import android.widget.Button;
  15.     //Class data members
  16.     Button mBtnULC=null, mBtnURC=null, mBtnLLC=null, mBtnLRC=null; //cascade declaration
  17.     Context mContext;
  18.     public static final String ERROR_STAMP = "NULL OBJECT";
  19.  
  20.     View.OnClickListener mClickHandler = new View.OnClickListener() {
  21.         @Override
  22.         public void onClick(View v) {
  23.             boolean bCautious = v instanceof Button;
  24.             if (bCautious){
  25.                 Button b = (Button)v;
  26.                 String strTheButtonText = b.getText().toString();
  27.                 Toast t = Toast.makeText(
  28.                     mContext,
  29.                     //"Button "+strTheButtonText+" was clicked!",
  30.                 strTheButtonText +
  31.                     " "+
  32.                     getString(R.string.strButtonResponse),
  33.                     Toast.LENGTH_LONG
  34.                 );
  35.                 t.setGravity(
  36.                     Gravity.CENTER,
  37.                     0,
  38.                     0
  39.                 );
  40.                 t.show();
  41.             }//if bCautious
  42.         }//onClick
  43.     }; //mClickHandler
  44.  
  45.     @Override
  46.     protected void onCreate(Bundle savedInstanceState) {
  47.         super.onCreate(savedInstanceState);
  48.         //setContentView(R.layout.activity_main);
  49.         //CTRL+SPACE - all available autocomplete options
  50.         setContentView(R.layout.rl_four_corners_v2);
  51.         //setContentView(R.layout.cl_four_corners);
  52.  
  53.         init();
  54.     }//onCreate
  55.  
  56.     public void someMethodInTheActivityClass (View pV){
  57.         Toast t = Toast.makeText(
  58.             this,
  59.             //"hello",
  60.             ((Button)pV).getText().toString(),
  61.             Toast.LENGTH_LONG
  62.         );
  63.         t.show();
  64.     }//someMethodInTheActivityClass
  65.  
  66.     void init(){
  67.         this.mContext = this;
  68.         //1 - bindings between XML elements and class data members
  69.         //get rid of the null status
  70.         mBtnULC = findViewById(R.id.idBtnULC);
  71.         mBtnURC = findViewById(R.id.idBtnURC);
  72.         mBtnLLC = findViewById(R.id.idBtnLLC);
  73.         mBtnLRC = findViewById(R.id.idBtnLRC);
  74.  
  75.         //quality control
  76.         Button[] aRelevantButtons = {
  77.             mBtnLLC, mBtnLRC, mBtnURC, mBtnULC
  78.         };
  79.         for (Button b : aRelevantButtons){
  80.             boolean bNull = b==null;
  81.             if (bNull){
  82.                 Log.e(
  83.                     ERROR_STAMP,
  84.                     "Button found null. Aborting."
  85.                 );
  86.                 finish(); //terminates the Activity
  87.             }//if
  88.         }//for
  89.  
  90.         //2 - via the Java bindings, assign behavior to the XML elements
  91.         mBtnULC.setOnClickListener(this); //useless
  92.         mBtnURC.setOnClickListener(this); //useless
  93.         mBtnLLC.setOnClickListener(mClickHandler); //useless
  94.  
  95.         makeAllButtonRespondToClickViaSameHandler(
  96.             aRelevantButtons,
  97.             mClickHandler //Buttons will respond via Class's mClickHandler
  98.             //this //Buttons will respond via Class's onclick
  99.         );
  100.     }//init
  101.  
  102.     void makeAllButtonRespondToClickViaSameHandler(
  103.         Button[] pButtonsThatShouldBehaveViaTheSameHandler,
  104.         View.OnClickListener pHandler
  105.     ){
  106.         for (Button b : pButtonsThatShouldBehaveViaTheSameHandler){
  107.             b.setOnClickListener(pHandler);
  108.         }//for
  109.     }//makeAllButtonRespondToClickViaSameHandler
  110.  
  111.     /*
  112.     the contractual interface View.OnClickListener
  113.     comes with a single obligation
  114.     to write the onClick method
  115.  
  116.     The class itself now provides a solution named "onClick"
  117.     for whatever View object to respond to a click
  118.      */
  119.     @Override
  120.     public void onClick(View v) {
  121.         int iWhichViewWasClicked = v.getId();
  122.         switch(iWhichViewWasClicked){
  123.             case R.id.idBtnULC:
  124.             case R.id.idBtnURC:
  125.             case R.id.idBtnLLC:
  126.             case R.id.idBtnLRC:
  127.                 Toast t = Toast.makeText(
  128.                     mContext,//Context
  129.                     "via implements interface",//String (the message)
  130.                     Toast.LENGTH_LONG //int (the message's duration)
  131.                 );
  132.                 t.show();
  133.         }//switch
  134.     }//onClick
  135. }//MainActivity
Add Comment
Please, Sign In to add comment