Project name: P0101 Listener Build Target: Android


has an interface specified



Download 136,99 Kb.
bet3/3
Sana17.01.2022
Hajmi136,99 Kb.
#382110
1   2   3
has an interface specified, but there is no implementation of the methods of this interface. Let's fix this with Eclipse. Hover over MainAcivity and select Add unimplemented methods


Eclipse will add the familiar onClick method. Only now this method will be implemented in the Activity, and not in a separate handler object. Accordingly, the Activity will act as a handler.

Fill in the method exactly as before. Nothing has changed. It is also served the View (on which the event occurred) as input. We will use the Id to determine which View it is and perform the corresponding actions:

1

2

3



4

5

6



7

8

9



10

11

12



13

public void onClick(View v) {

//by id , we determine the button that called this

switch (v) handler.getId()) {

case R. id. btnOk:

/ / кнопка OK button

TVout.setText("OK button pressed);

break;

case R. id. btnCancel:



/ / кнопкаCancel button

TVout. setText("Нажата кнопкаCancel " button is pressed);

break;

}

}



  

All that remains onCreateis to assign a handler to the buttons in the onCreate method. This will be the this object, i.e. the current MainActivity object.



1

2


btnOk.setOnClickListener(this);

btnCancel.setOnClickListener(this);



 

With this implementation, we did not create any extra objects (the Activity is created anyway) and the memory consumption is minimal, this is the recommended method. But this method may seem complicated and incomprehensible, especially if you have little experience in object-oriented programming. In this case, use the implementation that you understand and are comfortable with. And with time and experience, understanding will definitely come.

Full Code:

1

2

3



4

5

6



7

8

9



10

11

12



13

14

15



16

17

18



19

20

21



22

23

24



25

26

27



28

29

30



31

32

33



34

35

36



37

38


public class MainActivity extends Activity implements OnClickListener {

  

   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);


// find View-elements

tvOut = (TextView) findViewById(R. id.tvOut);

btnOk = (Button) findViewById(R. id.btnOk);

btnCancel = (Button) findViewById(R. id.btnCancel);


// assign the handler to the buttons

btnOk.setOnClickListener(this);

btnCancel.setOnClickListener(this);

}
@Override

public void onClick(View v) {

// by id opredeliaet the button that caused this handler

switch (V.getId()) {

case R. id. btnOk:

/ / кнопка OK button

TVout.setText("OK button pressed);

break;

case R. id. btnCancel:



/ / кнопкаCancel button

TVout. setText(" Cancel " button pressed);

break;

}

}



 

 }


 

The simplest implementation of a handler

There is another way to implement it. In the layoutfile (main.xml) when describing the button, write:

1

2

3



4

5

6



7



 

In other words, we use onClick. In it, specify the name of the method from the Activity. This method will work when you click on the button.

Next, we add this method to the Activity (MainActivity.java). Requirements for the method: public, void and accepts View as input:

1

2

3



public void onClickStart(View v) {

//button click actions



}

 In the method, specify the actions you need, and they will be performed when the button is clicked. 

Download 136,99 Kb.

Do'stlaringiz bilan baham:
1   2   3




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