Skip to main content

Basic onClickListener



Its a very basic example to implement onClickListener for Button:
- Button A, define onClickListener as anonymous class.
- Button B1 and B2: define onClickListener class.
- Button C1 and C2: assign callback function in XML.


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_id="@+id/background"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
android_onClick="onBackgroundClickedCallback"
tools_context="com.blogspot.android_er.androidclicklistener.MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<Button
android_id="@+id/buttona"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Button A: anonymous onClickListener"/>

<Button
android_id="@+id/buttonb1"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Button B1: onClickListener"/>

<Button
android_id="@+id/buttonb2"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Button B2: onClickListener"/>

<Button
android_id="@+id/buttonc1"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_onClick="onClickCallback"
android_text="Button C1: define callback in XML"/>

<Button
android_id="@+id/buttonc2"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_onClick="onClickCallback"
android_text="Button C2: define callback in XML"/>

<TextView
android_id="@+id/info"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textSize="28dp"/>
</LinearLayout>


MainActivity.java
package com.blogspot.android_er.androidclicklistener;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button btnA, btnB1, btnB2, btnC1, btnC2;
TextView textInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnA = (Button)findViewById(R.id.buttona);
btnB1 = (Button)findViewById(R.id.buttonb1);
btnB2 = (Button)findViewById(R.id.buttonb2);
btnC1 = (Button)findViewById(R.id.buttonc1);
btnC2 = (Button)findViewById(R.id.buttonc2);
textInfo = (TextView) findViewById(R.id.info);

btnA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"Button A Clicked: implement anonymous onClickListener",
Toast.LENGTH_LONG).show();
textInfo.setText(v.toString());
}
});

btnB1.setOnClickListener(btnBOnClickListener);
btnB2.setOnClickListener(btnBOnClickListener);
}

//set by calling setOnClickListener()
View.OnClickListener btnBOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {

if(v.getId() == R.id.buttonb1){
Toast.makeText(MainActivity.this,
"Button B1 Clicked: implement onClickListener",
Toast.LENGTH_LONG).show();
}else if((v.getId() == R.id.buttonb2)){
Toast.makeText(MainActivity.this,
"Button B2 Clicked: implement onClickListener",
Toast.LENGTH_LONG).show();
}

textInfo.setText(v.toString());
}
};

//define in XML
public void onClickCallback(View v){
if(v.getId() == R.id.buttonc1){
Toast.makeText(MainActivity.this,
"Button C1 Clicked: callback assign in XML",
Toast.LENGTH_LONG).show();
}else if((v.getId() == R.id.buttonc2)){
Toast.makeText(MainActivity.this,
"Button C2 Clicked: callback assign in XML",
Toast.LENGTH_LONG).show();
}

textInfo.setText(v.toString());
}

//define in XML
public void onBackgroundClickedCallback(View v){
Toast.makeText(MainActivity.this,
"Background Clicked",
Toast.LENGTH_LONG).show();
textInfo.setText(v.toString());
}
}


Popular posts from this blog

Baixar PS1 Crash Bandicoot 2 Cortex Strikes Back

PARTE 1: http://www.megaupload.com/?d=1AVO7X8T PARTE 2: http://www.megaupload.com/?d=WUBY6EAH

Barbarian Level 70 Paragon Level 191

Ive been thoroughly enjoying Reaper of Souls, so much so that Ive skipped posting here for some time (sorry, guys) and just concentrated on leveling-up my barbarian character. As you can see from the screenshot below, Ive been spending quite some time transmogrifying and buying up dyes making sure everythings coordinated and shit. Right now my barbarian is the most powerful (i.e., the one able to dish out the most damage) among my characters but I have a feeling that my crusader is going to overtake him really fast (at least until he levels up to 70 - just 2 more levels to go). Also, hes the sugar daddy of the bunch, the one tasked to make the most money for spending by the group. Ive given him some rest in the last couple of weeks however, preferring to adventure with my crusader and exploring his different abilities. Ill be getting him back on track soon enough though as the barbarian is my favorite class. Im currently playing at Torment II but I hope to progress to Torment III as so...

Baixar Filme Download Quadrilogia Veloses e Furiosos – Dublado

Sinopse : Eles t�m muita adrenalina e m�quinas poderosas, mas acima de tudo, eles possuem a extrema necessidade de velocidade. Nas ruas de Los Angeles, toda noite Dominic Toretto (Vin Diesel) transforma seus desafiantes em p�. Mas agora, h� um novo furor nas ruas. Eles sabem que ele � dur�o e r�pido, mas o que n�o sabem � que ele � um detetive determinado e veloz (Paul Walker). Com intensa a��o e velocidade, espantosas acrobacias e com o p� fundo no acelerador, este ataque furioso o colocar� no banco do motorista e o desafiar� a exceder os seus limites. Lan�amento : 2001 a 2008 Dura��o : 100 min apx. Cada Qualidade : DVDRip �udio : 10 V�deo : 10 Formato : RMVB Tamanho : 350 Mb apx. Cada �dioma : Portugu�s Veloses e Furiosos 1 >> Megaupload Veloses e Furiosos 2 >> Megaupload Veloses e Furiosos 3 >> Megaupload Veloses e Furiosos 4 >> Megaupload