Mundarija:

HelloWorld AndroidStudio chiqish tugmasi bilan: 4 qadam
HelloWorld AndroidStudio chiqish tugmasi bilan: 4 qadam

Video: HelloWorld AndroidStudio chiqish tugmasi bilan: 4 qadam

Video: HelloWorld AndroidStudio chiqish tugmasi bilan: 4 qadam
Video: Hello World 2024, Noyabr
Anonim
HelloWorld AndroidStudio chiqish tugmasi bilan
HelloWorld AndroidStudio chiqish tugmasi bilan
HelloWorld AndroidStudio chiqish tugmasi bilan
HelloWorld AndroidStudio chiqish tugmasi bilan

Bu ko'rsatma sizga "Hello World" matnini ko'rsatadigan Android ilovasini qanday yaratishni o'rgatadi va undan chiqish uchun tugmani bosing.

1 -qadam: Yangi loyiha yaratish

Android Studio -ni oching va yangi loyiha yarating. Yangi loyihangizni HelloWorld deb nomlang va "Bo'sh harakatlar" ni qo'shing.

2 -qadam: Activity_main.xml -ni tahrirlang

Res> layout> activity_main.xml ichida yangi matn ko'rinishi va tugmani (quyida ko'rsatilganidek) qo'shing.

android: id = "@+id/btn_logout" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@+id/textView" android: layout_centerHorizontal = "rost" android: text = "Chiqish" android: textColor = "@color/black"/>

Biz matn uchun ranglardan foydalanganimiz uchun color.xml -ga yangi manba qo'shing. Res> qadriyatlar> colors.xml -ga o'ting va quyidagi kodni qo'shing

#0d0c0c>

3 -qadam: Endi MainActivity.java -ni tahrir qiling

MainActivity.javadagi OnCreate () usuliga quyidagi kodni qo'shing

Biz tugmachaga OnClickListener funktsiyasini qo'shamiz, shuning uchun foydalanuvchi tugmani bosganida "Chiqishni xohlaysizmi?" Ogohlantiruvi bilan dialog oynasi ochiladi. Foydalanuvchiga chiqish uchun "Ha …" va dialogni yopib, asosiy sahifaga qaytish uchun "Yo'q …" ikkita variant taqdim etiladi.

Btnlogout tugmasi = (Button) findViewById (R.id.btn_logout);

btnlogout.setOnClickListener (yangi View. OnClickListener () {@Override public void onClick (View view) {final AlertDialog. Builder builder = new AlertDialog. Builder (MainActivity.this); builder.setTitle ("Exit"); "Chiqishni xohlaysizmi?"); Builder.setPositiveButton ("Ha. Hozir chiq!", Yangi DialogInterface. OnClickListener () {@Override public void onClick (DialogInterface dialogInterface, int i) finish ();}}); builder.setNegativeButton ("Hozir emas", yangi DialogInterface. OnClickListener () {@Override public void onClick (DialogInterface dialogInterface, int i) dialogInterface.dismiss ();}}); AlertDialog dialog = builder.create (); dialog.show ();}});

4 -qadam: tugating

Endi siz dasturni ishga tushirishingiz mumkin.

Omad

To'liq kod bu erda:

Tavsiya: