am_dot_com

DDM 2022-10-19

Oct 19th, 2022 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <Button
  7. android:layout_alignParentTop="true"
  8. android:layout_alignParentLeft="true"
  9. android:id="@+id/idBtnCSE"
  10. android:text="@string/strBtnCSE"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"/>
  13.  
  14. <Button
  15. android:layout_alignParentTop="true"
  16. android:layout_alignParentRight="true"
  17. android:id="@+id/idBtnCSD"
  18. android:text="@string/strBtnCSD"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"/>
  21.  
  22. <Button
  23. android:layout_alignParentLeft="true"
  24. android:layout_alignParentBottom="true"
  25. android:id="@+id/idBtnCIE"
  26. android:text="@string/strBtnCIE"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"/>
  29.  
  30. <Button
  31. android:layout_alignParentBottom="true"
  32. android:layout_alignParentRight="true"
  33. android:id="@+id/idBtnCID"
  34. android:text="@string/strBtnCID"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"/>
  37. </RelativeLayout>
  38. ********************************
  39. package com.joythis.android.fourcorners;
  40.  
  41. import androidx.appcompat.app.AppCompatActivity;
  42.  
  43. import android.content.Context;
  44. import android.os.Bundle;
  45. import android.util.Log;
  46. import android.view.Gravity;
  47. import android.view.View;
  48. import android.widget.Button;
  49. import android.widget.Toast;
  50.  
  51. public class MainActivity
  52. extends AppCompatActivity // class inheritance
  53. implements View.OnClickListener // interface assumption
  54. {
  55. AmUtil mUtil; // cliente de AmUtil
  56.  
  57. public final static String STAMP = "@MainActivity";
  58.  
  59. /*
  60. Todos os elementos de layout que sejam relevantes para a
  61. programação (para comportamento) têm que ter um id no Layout
  62. e devem ter um data member correspondente na class da Activity
  63. que carregar o Layout
  64. */
  65. Button mBtnCSE; //null
  66. Button mBtnCSD; //null
  67. Button mBtnCIE; //null
  68. Button mBtnCID; //null
  69.  
  70. Context mContext; //null
  71.  
  72. // tipo nome
  73. Button.OnClickListener mBtnClickHandler =
  74. new View.OnClickListener() {
  75. @Override
  76. public void onClick(View v) {
  77. Button btnWasClicked = (Button)v;
  78. int id = v.getId();
  79.  
  80. String strMsg = "Hello via mBtnClickHandler from ";
  81. switch(id){
  82. case R.id.idBtnCSE:
  83. strMsg += strMsg.format("CSE");
  84. break;
  85. case R.id.idBtnCSD:
  86. strMsg += strMsg.format("CSD");
  87. break;
  88. case R.id.idBtnCIE:
  89. strMsg += strMsg.format(
  90. btnWasClicked.getText().toString()
  91. );
  92. break;
  93. case R.id.idBtnCID:
  94. strMsg += strMsg.format(
  95. btnWasClicked.getText().toString()
  96. );
  97. break;
  98. } // switch
  99. mUtil.fb(strMsg);
  100. /*
  101. AmUtil.fbs(
  102. this, // this NÃO É a Activity
  103. strMsg
  104. );
  105. */
  106. } // onClick
  107. }; //mBtnClickHandler
  108.  
  109. @Override
  110. protected void onCreate(Bundle savedInstanceState) {
  111. super.onCreate(savedInstanceState);
  112. // setContentView(R.layout.activity_main);
  113. setContentView(R.layout.ll_four_corners_v1);
  114.  
  115. init();
  116. } // onCreate
  117.  
  118. void init(){
  119. // inicializações dos data members
  120. mUtil = new AmUtil(this);
  121.  
  122. mContext = this;
  123. mBtnCSE = findViewById(R.id.idBtnCSE);
  124. mBtnCSD = findViewById(R.id.idBtnCSD);
  125. mBtnCIE = findViewById(R.id.idBtnCIE);
  126. mBtnCID = findViewById(R.id.idBtnCID);
  127.  
  128. // controlo de qualidade
  129. Object[] theRelevant = {
  130. mContext,
  131. mBtnCSE,
  132. mBtnCSD,
  133. mBtnCIE,
  134. mBtnCID
  135. };
  136. boolean bFoundANull = false;
  137. for (Object r : theRelevant){
  138. bFoundANull = r==null;
  139. if (bFoundANull){
  140. // mensagem
  141. Log.e(
  142. STAMP,
  143. "There is 1+ null relevant objects. Aborting."
  144. );
  145. this.finish();
  146. return;
  147. }
  148. } // for
  149.  
  150. // conferir comportamentos
  151. Button[] buttonsWithSharedBehavior = {
  152. //mBtnCSE, mBtnCSD, mBtnCIE, mBtnCID
  153. //mBtnCSE - comporta-se via onClick do XML
  154. mBtnCSD, mBtnCIE
  155. // mBtnCID - comporta-se via onClick da class
  156. };
  157. /*
  158. mBtnCSE.setOnClickListener(mBtnClickHandler);
  159. mBtnCID.setOnClickListener(mBtnClickHandler);
  160. */
  161. for (Button b : buttonsWithSharedBehavior)
  162. b.setOnClickListener
  163. (
  164. //this
  165. mBtnClickHandler
  166. );
  167.  
  168. mBtnCID.setOnClickListener(this);
  169. } // init
  170.  
  171. public void responderAoClick(View pV){
  172. Toast t = Toast.makeText(
  173. this,
  174. "Responding to onClick as set in XML\n"+
  175. getResources().getString(R.string.strBtnCSE),
  176. Toast.LENGTH_SHORT
  177. );
  178. t.setGravity(
  179. Gravity.CENTER,
  180. 0,
  181. 0
  182. );
  183. t.show();
  184. } // responderAoClick
  185.  
  186. @Override
  187. public void onClick(View v) {
  188. mUtil.fb(
  189. "handling via class implements\n"+
  190. ((Button)v).getText().toString()
  191. );
  192. } //onClick
  193. } // class MainActivity
Advertisement
Add Comment
Please, Sign In to add comment