Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"
- android:id="@+id/idBtnCSE"
- android:text="@string/strBtnCSE"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <Button
- android:layout_alignParentTop="true"
- android:layout_alignParentRight="true"
- android:id="@+id/idBtnCSD"
- android:text="@string/strBtnCSD"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <Button
- android:layout_alignParentLeft="true"
- android:layout_alignParentBottom="true"
- android:id="@+id/idBtnCIE"
- android:text="@string/strBtnCIE"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <Button
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:id="@+id/idBtnCID"
- android:text="@string/strBtnCID"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </RelativeLayout>
- ********************************
- package com.joythis.android.fourcorners;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity
- extends AppCompatActivity // class inheritance
- implements View.OnClickListener // interface assumption
- {
- AmUtil mUtil; // cliente de AmUtil
- public final static String STAMP = "@MainActivity";
- /*
- Todos os elementos de layout que sejam relevantes para a
- programação (para comportamento) têm que ter um id no Layout
- e devem ter um data member correspondente na class da Activity
- que carregar o Layout
- */
- Button mBtnCSE; //null
- Button mBtnCSD; //null
- Button mBtnCIE; //null
- Button mBtnCID; //null
- Context mContext; //null
- // tipo nome
- Button.OnClickListener mBtnClickHandler =
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Button btnWasClicked = (Button)v;
- int id = v.getId();
- String strMsg = "Hello via mBtnClickHandler from ";
- switch(id){
- case R.id.idBtnCSE:
- strMsg += strMsg.format("CSE");
- break;
- case R.id.idBtnCSD:
- strMsg += strMsg.format("CSD");
- break;
- case R.id.idBtnCIE:
- strMsg += strMsg.format(
- btnWasClicked.getText().toString()
- );
- break;
- case R.id.idBtnCID:
- strMsg += strMsg.format(
- btnWasClicked.getText().toString()
- );
- break;
- } // switch
- mUtil.fb(strMsg);
- /*
- AmUtil.fbs(
- this, // this NÃO É a Activity
- strMsg
- );
- */
- } // onClick
- }; //mBtnClickHandler
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.activity_main);
- setContentView(R.layout.ll_four_corners_v1);
- init();
- } // onCreate
- void init(){
- // inicializações dos data members
- mUtil = new AmUtil(this);
- mContext = this;
- mBtnCSE = findViewById(R.id.idBtnCSE);
- mBtnCSD = findViewById(R.id.idBtnCSD);
- mBtnCIE = findViewById(R.id.idBtnCIE);
- mBtnCID = findViewById(R.id.idBtnCID);
- // controlo de qualidade
- Object[] theRelevant = {
- mContext,
- mBtnCSE,
- mBtnCSD,
- mBtnCIE,
- mBtnCID
- };
- boolean bFoundANull = false;
- for (Object r : theRelevant){
- bFoundANull = r==null;
- if (bFoundANull){
- // mensagem
- Log.e(
- STAMP,
- "There is 1+ null relevant objects. Aborting."
- );
- this.finish();
- return;
- }
- } // for
- // conferir comportamentos
- Button[] buttonsWithSharedBehavior = {
- //mBtnCSE, mBtnCSD, mBtnCIE, mBtnCID
- //mBtnCSE - comporta-se via onClick do XML
- mBtnCSD, mBtnCIE
- // mBtnCID - comporta-se via onClick da class
- };
- /*
- mBtnCSE.setOnClickListener(mBtnClickHandler);
- mBtnCID.setOnClickListener(mBtnClickHandler);
- */
- for (Button b : buttonsWithSharedBehavior)
- b.setOnClickListener
- (
- //this
- mBtnClickHandler
- );
- mBtnCID.setOnClickListener(this);
- } // init
- public void responderAoClick(View pV){
- Toast t = Toast.makeText(
- this,
- "Responding to onClick as set in XML\n"+
- getResources().getString(R.string.strBtnCSE),
- Toast.LENGTH_SHORT
- );
- t.setGravity(
- Gravity.CENTER,
- 0,
- 0
- );
- t.show();
- } // responderAoClick
- @Override
- public void onClick(View v) {
- mUtil.fb(
- "handling via class implements\n"+
- ((Button)v).getText().toString()
- );
- } //onClick
- } // class MainActivity
Advertisement
Add Comment
Please, Sign In to add comment