Android, code optimization -


Please do not judge fairly to post me, still newbie I'm developing an Android application in programming Pass code is duplicated. Is there a solution to optimize these lines of code? Thanks

Button BTN1 = (button) dialog.findViewById (R.id.btn1); Button BTN2 = (button) dialog.findViewById (R.id.btn2); Button BTN3 = (button) dialog.findViewById (R.id.btn3); Button BTN4 = (button) dialog.findViewById (R.id.btn4); Button BTN 5 = (button) dialog.findViewById (R.id.btn5); Button BTN6 = (Button) dialog.findViewById (R.id.btn6); Button BT7 = (Button) dialog.findViewById (R.id.btn7); Button BTN8 = (button) dialog.findViewById (R.id.btn8); Button BTN 9 = (button) dialog.findViewById (R.id.btn9); Btn1.setOnClickListener (listener); Btn2.setOnClickListener (listener); Btn3.setOnClickListener (listener); Btn4.setOnClickListener (listener); Btn5.setOnClickListener (listener); Btn6.setOnClickListener (listener); Btn7.setOnClickListener (listener); Btn8.setOnClickListener (listener); Btn9.setOnClickListener (listener);

add an Android: onClick = "onButtonClick" listener For each button in layout xml

  & lt; Android: layout = width = "wrap_content" android: layout_height = "wrap_content" android: text = "button1" Android: layout_margin = "10dp" Android: onClick = "onButtonClick" / & gt; Android: id = "@ + id / button1" Android: layout_width = "wrap_content" Gt; To add a click on the button, click Add a public method  onButtonClick  in your  activity . When  onButtonClick  is called, depending on the button ID, then do your other stuff 

  Buttonton on Public Zero (see View) {int id = view.getId (); Switch (ID) {Case R.D. button 1: // Make your luggage; break; Case R.id.button2: // Break Your Goods; }}  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -