Hodisalar bilan ishlashni optimizatsiya qilamiz.

Bundan oldingi darsimiz hodisalar bilan ishlashni ko’rdik. Ushbu darsimizda esa view elementlarining hodisalari bilan ishlashning optimal variantini o’rganamiz. Bundan oldingi darsimizdagi kamchiligi OnclickListener classimizning har bir element uchun obyektini yaratib turub uning metodiga bajaradigan vazifasini kiritish edi. Ushbu darsimizda esa OnClickListener classimizning bir obyekytini yaratib uning onClick metodi bilan barcha view elementlarimiz bilan ishlashni o’rganamiz.

Application name: Button_event_optimal
Project Name: Button_event_optimal
Package Name: uz.projects.button_event_optimal
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_height=“match_parent”
android:layout_width=“match_parent”
android:orientation=“horizontal”>
<LinearLayout
android:id=“@+id/linearLayout1”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:layout_width=“match_parent”
android:layout_margin=“30dp”>

<TextView
android:layout_width=“wrap_content”
android:text=“TextView”
android:layout_height=“wrap_content”
android:id=“@+id/tvOut”
android:layout_gravity=“center_horizontal”
android:layout_marginBottom=“50dp”>
</TextView>
<Button
android:layout_height=“wrap_content”
android:layout_gravity=“center_horizontal”
android:id=“@+id/btnOk”
android:text=“OK”
android:layout_width=“100dp”>
</Button>

<Button
android:layout_height=“wrap_content”
android:layout_gravity=“center_horizontal”
android:id=“@+id/btnCancel”
android:text=“Cancel”
android:layout_width=“100dp”>

</Button>
</LinearLayout>
</LinearLayout>

MainActivity classimizga o’tib TextView va ikkita Button classlarini obyektlarini yaratib olib ularni view elementlari bilan bog’laymiz. Hamda onClickListener obyektini ham yaratib olamiz.

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 methodimiz=new OnClickListener() {
@Override
public void onClick(View v) {                           // TODO Auto-generated method stub
}
};
}
@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;
}

}
Endi esa onClickListener obyektimizning onClick metodiga o’tamiz. Method bajarishi kerak bo’lgan vazifa shuki u view elemnti bosilgan idnini aniqlab uning vazifasini bajarishdir. O’tgan darslardan ma’lumki view elemtlarining idsi quydagicha edi (button misolida) R.id.btnOk . onClickListener obyektimizning onClick metodida bitta kiruvchi View parameter bor. Biz view parametrining getId metodidan foydalanib view elemntlarida bosilgan elemntni idsini olamiz va uni switch operatori orqaliy qaysi elemnt ekanligini aniqlab unga vazifa beramiz.

public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnOk:
tvOut.setText(“Ok tugmasini bosdingiz”);
break;
case R.id.btnCancel :
tvOut.setText(“Siz cancel tugmasini bosdingiz”);
break;
}
}

Ok(ok) va Bekor qilish(Cancel) tugmalarimizning setOnClickListener metodiga OnClickListener obyektimizni ko’rsatamiz.

btnOk.setOnClickListener(methodimiz);
btnCancel.setOnClickListener(methodimiz);

AVD da tekshiramiz.

Full code:

package uz.projects.button_event_optimal;
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 methodimiz=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnOk:
tvOut.setText(“Ok tugmasini bosdingiz”);
break;
case R.id.btnCancel :
tvOut.setText(“Siz cancel tugmasini bosdingiz”);
break;
}
}
};

btnOk.setOnClickListener(methodimiz);
btnCancel.setOnClickListener(methodimiz);
}
@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;
}
}
Endi esa bundanda optimal variantni ko’rib chiqamiz.

Agar e’tibor bergan bo’lsangzi MainActivity classimiz  Activity classiga voris bo’layotgan edi va biz Implements OnClickListener deb qo’shamiz va uning metodini yaratamiz!

Quydagicha OnclickListener classimizning metodi yaratiladi.
public void onClick(View v) {
// TODO Auto-generated method stub
}
Ushbu metodda ham getId metodidan foydalanib view elementlari bilan ishlaymiz.
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnOk:
tvOut.setText(“Ok tugmasini bosdingiz”);
break;
case R.id.btnCancel :
tvOut.setText(“Siz cancel tugmasini bosdingiz”);
break;
}
}
Endi esa onCreate metodimizda ok va cancel tugmalarimizning setOnClickListener metodiga this qiymatini beramiz.

btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);

Appni bemalol ishga tushurib sinab ko’rishingiz mumkin.
Full code:
package uz.projects.button_event_optimal;
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 implements OnClickListener{
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);
btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}
@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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnOk:
tvOut.setText(“Ok tugmasini bosdingiz”);
break;
case R.id.btnCancel :
tvOut.setText(“Siz cancel tugmasini bosdingiz”);
break;
}
}
}
Elementlarning hodisalari bilan ishlashning eng oddiy yo’li:
<Button
android:id=“@+id/btnOk
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:onClick=“BizningMethod ”
android:text=“ok”>
</Button>

Bu yerda button elementining onClick xususiyatiga metodimizning nomini qiymat qilib
beramiz.
MainActivity classimizga o’tib  BizningMethod deb metod yaratamiz.

public void BizningMethod(View v) {
tvOut.setText(“Siz button hodisasi bilan ishlashning eng oddiy yo’lidan foydalandingiz “);
}

Full code:

package uz.projects.button_event_optimal;
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);
}
@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;
}
public void BizningMethod (View v)
{
tvOut.setText(“Siz button hodisasi bilan ishlashning eng oddiy yolidan foydalandingiz”);
}
}

 

 

One Reply to “Hodisalar bilan ishlashni optimizatsiya qilamiz.”

Comments are closed.