Android Application Development Cookbook: 93 Recipes for Building Winning Apps



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

 
Recipe 2.1 using Buttons

43
public void onClick(View view)
{
Toast.makeText(getBaseContext(),
((Button) view).getText() + " was clicked!",
Toast.LENGTH_LONG).show();
}
};
You then set the two buttons’ click event handler to this class:
//---the two buttons are wired to the same event handler--- 
Button btn4 = (Button) findViewById(R.id.button4);
btn4.setOnClickListener(btnListener);
Button btn5 = (Button) findViewById(R.id.button5); 
btn5.setOnClickListener(btnListener);
The 
ToggleButton
behaves just like the 
Button
except that it displays a blue line indicating whether 
it is toggled: 
public void onToggle(View view) {
ToggleButton btn = (ToggleButton) view;
Toast.makeText(this, "Toggle mode: " + btn.isChecked(), 
Toast.LENGTH_SHORT).show();
}
You can check whether a 
ToggleButton
is toggled by checking its 
isChecked()
method. 
Figure 2-1 shows the buttons in action.
Figure 2-1
www.it-ebooks.info


44

ChAPter 2
using Views to design the user interFAce
reCiPe 2.2 
using iMAge buttons
Android Versions
Level 1 and above
permissions
None
source code to download from Wrox.com
ImageButtons.zip
The previous recipe showed the 
Button
and 
ToggleButton
in action and the various ways to handle 
their events. This recipe describes how to display images on buttons. As the saying goes, a picture is 
worth a thousand words. Hence, sometimes it would be more intuitive and descriptive to display a 
picture on top of a button rather than words. 
solution
Consider the following code snippet in the 
activity_main.xml
file:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:onClick="onToggle" />
android:id="@+id/imageTextButton1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:text="Android"/>
android:id="@+id/imageTextButton2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:text="Android"/>
android:id="@+id/imageTextButton3" 
android:layout_width="wrap_content" 
www.it-ebooks.info



Download 32,34 Mb.

Do'stlaringiz bilan baham:
1   ...   41   42   43   44   45   46   47   48   ...   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