Android OS - Button

Assalomu alaykum bo'lajak dasturchilar. Google Android OS lar uchun dastur yozishni o'rganishda davom etamiz.
Bugungi darsimizda “Button”-tugma komponentasi bilan tanishib chiqamiz.
Button tugmachasi dasturlashda juda ham ko'p ishlatiladigan komponentalardan biri hisoblanadi.

Yangi project hosil qilamiz File-->New-->Android Project
/res/layout/main.xml main.xml faylini ochamiz va Button komponentasini joylashtiramiz
alt


main.xml:


android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

android:id="@+id/button1"
android:layout_width="149dp"
android:layout_height="wrap_content"
android:text="Meni bos" android:textColor="#ff0000"                               android:layout_gravity="center"/>


res/layout/ ni ichida yana bitta xml fayl yaratamiz

form2.xml faylini yaratganimizdan so'ng uning ichiga Liner Layout  komponentasini joylashtiramiz va yana TextView va ImageView ni ham joylashtiramiz

form2.xml:

android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Bu ikkinchi layout(form)"
android:textColor="#ff00ff"
android:textSize="30dp" />

android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.41"
android:src="@drawable/ic_launcher" />



Asosiy Activity ni ochamiz

Joylashtirgan Button konponentamizni asosiy activity da tanitib olamiz:

Button button = (Button)findViewById(R.id.button1);

Button konmponentasining setOnClickListener();  methodi mavjud bo'lib, bu method button bosilgan holatda bajariladigan amallar hisoblanadi.
button.setOnClickListener(new OnClickListener() {          
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
});


OnClickListener() ni tagiga qizil chiziq chizib turgan bo'lsa quyidagini amalga oshiring. Sichqonchani OnClickListener() ni ustiga olib boramiz va:

Import'OnClickListener'(android.view.View)

shuni bosamiz. Bu yerda Button konponentasini ishlatishimiz uchun kerak bo'ladigan methodlarni va klasslarni ushbu dasturga yukalb, yani import qilib oladi.

Endi Buttonimiz bosilganda qandaydir amal bajarilish kerak bo'lsa
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

ushbu methodni ichiga yozamaiz barcha funksiya va amallarni.
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ButtonActivity.this,Form2Activity.class);
startActivity(intent);
}


Intent bilan keyingi darsimizda to'liqroq tanishib chiqamiz, hozircha nima uchun ishlatganimizni aytib ketishni o'zi yetarli.
Intent ni,  bir Activity dan ikkinchi Activity ga o'tish uchun ishlatamiz, yani ushbu dasturda Button tugmasi bosilganda o'zi turgan oynadan, ikkinchi yani form2 oynasiga o'tadi.
Bu yerda Form2Activity.class ni tagiga qizil chiziq chizilgan, chunki biza bunday class hozircha yo'q. Form2Activity.class ni ustiga sichqonchani olib boramiz va
Create class'Form2Activity'   shuni bosamiz, yani ushbu Form2Activity classini yaratamiz.
Bosganimizdan so'ng bitta oyna chiqadi  Superclass degan joyiga android.app.Activity ni kiritamiz va Finish tugmasini bosamiz.
Biz hozir Form2Activity classini yaratdik, ushbu faylni ochib uning ichiga quyidagilarni yozamiz
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.form2);
}

Bu yerda biz boshida yaratgan form2.xml layoutini tanitib turibdi, yani ushbu Activity ishga tushganda form2.xml asosiy forma hisoblanadi.

Keyingi qiladigan ishimiz AndroidManifiest.xml faylini ochin unga



ni qo'shib qo'yamiz, yani hozir biz dasturni ishga tushirsak hato saodir bo'ladi, yani Form2Activity ni ko'rmiydi, shuning uchun har bir yaratilgan yangi Activity AndroidManifiest.xml ga qo'shib quyish kerak bo'ladi.

AndroidManifiest.xml :


package="com.boboy.button1"
android:versionCode="1"
android:versionName="1.0" >



android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:name=".ButtonActivity"
android:label="@string/app_name" >









Endi dasturimizni ishga tushiramiz
alt
Dastur ishladi, “Meni bos” tugmasini bosamiz
alt

Ushbu dasturning to'liq kodini
-ko'chirib olishingiz mumkin


Popular posts from this blog

Rus tili. Rus tilini o'rganish. Rus tili grammatikasi.(Грамматика русского языка в иллюстрациях.)(djvu). Mylingvo

PHP va MySQL video kursi . HTML, CSS, PHP, MYSQL dan video kurslar.