Android Application Development Cookbook: 93 Recipes for Building Winning Apps


((Button) view).getText() + " was clicked!"



Download 32,34 Mb.
Pdf ko'rish
bet44/56
Sana26.02.2022
Hajmi32,34 Mb.
#470540
1   ...   40   41   42   43   44   45   46   47   ...   56
Bog'liq
Android Application Development Cookbook

((Button) view).getText() + " was clicked!",
Toast.LENGTH_LONG).show();
}
};
public void onClick(View view) {
Button btn = (Button) view;
Toast.makeText(this, btn.getText() + " was clicked!", 
Toast.LENGTH_SHORT).show();
}
public void onToggle(View view) {
ToggleButton btn = (ToggleButton) view;
Toast.makeText(this, "Toggle mode: " + btn.isChecked(), 
Toast.LENGTH_SHORT).show();
}
}
The first two buttons (Button 1 and Button 2) have no 
android:id
attributes in their declaration. 
Instead, they have the 
android:onClick
attribute, which is set to 
onClick
. The 
onClick
refers to 
the name of the method in the activity:
public void onClick(View view) {
Button btn = (Button) view;
Toast.makeText(this, btn.getText() + " was clicked!", 
Toast.LENGTH_SHORT).show();
}
When these two buttons are clicked, they both fire the same 
onClick()
method as shown above. 
You can programmatically differentiate them by typecasting the 
view
argument that is passed in 
through the method and examining their text value. 
The next button (Button 3) has the 
android:id
attribute set, but not the 
android:onClick
attribute. To programmatically handle the click event for the button, you need to get a refer-
ence to the button, call its 
setOnClickListener()
method, and then set it an instance of the 
OnClickListener
class. In the 
OnClickListener
class, implement the 
onClick()
method:
//---Button view---
Button btn = (Button) findViewById(R.id.button3); 
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(getBaseContext(), "Button 3 was clicked!", 
Toast.LENGTH_SHORT).show();
}
});
The next two buttons are similar to Button 3, but instead of creating an inner class to handle the 
event, you created an anonymous class:
//---create an anonymous class to act as a button click listener---
private OnClickListener btnListener = new OnClickListener() {
www.it-ebooks.info



Download 32,34 Mb.

Do'stlaringiz bilan baham:
1   ...   40   41   42   43   44   45   46   47   ...   56




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish