Button misolida hodisalar bilan ishlash! Android 6-dars


Ushbu darsimizda button tugmasi bosilganda hodisa yuz berishi haqida o’rganamiz. Hodisa bilan xususiyat tushunchalarini ko’pchilik boshlovchi dasturchilar unitib qo’yadi. Keling hozi men hammamizga tushunarli bo’lgan hayotiy misol orqali ushbu ikki tushunchani tushuntirib o’tsam. Nexia avtomoashinasi uning xususiyatlari rangi, hajmi, oynalari,… va uni hodisalariga esa gaz pedalini bosganda tezlanishning ortishi, tormoz pedalini bosganda avtomoshina harakatdan to’xtashi, signal tugmasi bosilganda ogohlantiruvchi tovush chiqarishi va hklar ni misol keltirishimiz mumkin. Biz shu vaqtgacha bo’lgan darslarimizda obyektlarni xususiyatlari bilan ishlashni o’rgandik. Demak darsimizning asosiy qismiga o’tsak, hodisa tushunchasi haqida yuqorida ozroq o’rgandik. Endi esa hodisa bilan Button tugmasi orqaliy ishlashni o’rganamiz.
Yangi loyiha hosil qilamiz:
Application name: Button_event
Project Name: Button_event
Package Name: uz.projects.button_event
Target SDK: API 17 Android 4.2(Jelly Bean)
Create Activity: MainActivity
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="30dp"
android:orientation="vertical">
<TextView
android:id="@+id/tvOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="50dp"
android:text="TextView">
</TextView>
<Button
android:id="@+id/btnOk"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="OK">
</Button>
<Button
android:id="@+id/btnCancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>


Bizda tekist uchun TextView va OK, Cancel Buttonlari mavjud. Appimizning ishlashi ishlashi quydagicha: OK tugmasi bosilganda TextViewga OK tugmasini bosdingiz va Cancel tugmasi bosilganda siz Cancel tugmasini bosdingiz deb chiqaramiz.
MainActivity.java classimizni ochamiz va onCreate metodimizga o’tib view elementlari bilan ishlash uchun ularning obyektini hosil qilib view elementlarini idsi bilan bog’laymiz.
public class MainActivity extends Activity {
TextView tvOut;
Button btnOk;
Button btnCancel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// найдем View-элементы
tvOut = (TextView) findViewById(R.id.tvOut);
btnOk = (Button) findViewById(R.id.btnOk);
btnCancel = (Button) findViewById(R.id.btnCancel);
}
}

Loyihani yangilaymiz(CTRL+SHIFT+O). OnClickListener metodini yaratamiz:

Va quydagicha hosil bo’ladi:

Metod1 obyektimizning onClick metodimizga qilanadigan vazivani kiritamiz ya’ni “Siz ok tugmasini bosdingiz” deb. Buning uchun tvOut.setText(“Siz ok tugmasini bosdingiz”); kiritishimiz kerak bo’aldi. Endi esa Ok tugmasini setOnClickListener metodiga onclicklistener classimizning obyektini ko’rsatamiz.
btnOk.setOnClickListener(method1);

Quydagicha kod hosil bo’lishi kerak!

Endi esa test qilib ko’ramiz!CTRL+F11

Ok tugmasini bosganimizdagi holat.

Endi OnclickListener classimizdan metod2 deb obyektini yaratib uning onClick metodiga Cancel tugmasini bosgandagi holat ni qilamiz. Bu harakatni ikkinchi bor misollarda ko’rsatishdan ko’ra full kodni quyida keltirmoqchiman.
package uz.projects.button_event;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView tvOut;
	Button btnOk;
	Button btnCancel;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tvOut = (TextView) findViewById(R.id.tvOut);
		btnOk = (Button) findViewById(R.id.btnOk);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		OnClickListener method1=new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				tvOut.setText("Siz ok tugmasini bosdingiz");
			}
		};
		OnClickListener metod2=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tvOut.setText("Siz cancel tugmasini bosdingiz");
			}
		};
		
		
		btnOk.setOnClickListener(method1);
		btnCancel.setOnClickListener(metod2);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


Ekran elementlari bilan kod orqali ishlash! 5-dars

Maqolani yozishda startandroid.ru saytidan foydalanildi.

Нет комментариев