Onclicklistener button byte

I installed 10 buttons in xml and added to the main activity. I show only 2. All buttons just change the color of the variable and the button. Is there a way to group all the buttons into one onclicklistener and use the switch to check which button was clicked. Using a separate onclicklistener is similar to highlighting the waist.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); f1 =(Button) findViewById(R.id.f1); f2 =(Button) findViewById(R.id.f2); f1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearbutton(); f1.setBackgroundColor(Color.RED); intbtnSelect=0; } }); f2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearbutton(); f2.setBackgroundColor(Color.RED); intbtnSelect=1; } }); 
+4
source share
8 answers

You can implement OnClickListener in your Activity . For instance,

 public class MyActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); f1 =(Button) findViewById(R.id.f1); f1.setOnClickListener(this); f2 =(Button) findViewById(R.id.f2); f2.setOnClickListener(this); } } 

They need to implement the onClick method and switch to view.getId ():

 @Override public void onClick(View v) { switch (v.getId()) { case R.id.f1: // your code here break; case R.id.f2: // your code here break; } } 
+6
source

in your xml, on all of your buttons:

 android:onclick="myClick" 

in your activity:

 public void onClick (View v){ Button clickedButton = (Button) v; //Do wathever you want with your Button clickedButton.setBackgroundColor(Color.RED); // and you could also switch(v.getId()) if really needed. } 

You can also implement View.OnClickListener, as suggested by blackbelt, it just depends on your preference.

I personally like to maintain cleaner Java code, but it's up to you!

There is nothing good / wrong with these two methods, I think.

0
source

Let your activity implement View.OnClickListener , and then you must add the onClick(View view) method.

When creating buttons, you simply add this line

button.setOnClickListener(this) , so whenever a button is clicked, it calls the onClick() method in your activity.

Inside the onClick() method, you can use view.getId() to find out which button was clicked.

Code:

// onCreate () - either dynamically create your own buttons, or find them using findViewById () and assign a click listener

 for(int i = 0; i < MAX_BUTTONS; i++){ // Create a button here button.setOnClickListener(this); } 

// onClick ()

 switch(view.getId(){ case R.id.btn_one: // do something break; ... } 
0
source
  ****just implement View.OnClickListener and override the onClick(View v) as public class YourActivity implements Activity implements View.OnClickListener { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); f1 =(Button) findViewById(R.id.f1); f2 =(Button) findViewById(R.id.f2); f1.setOnClickListener(this); f2.setOnClickListener(this); @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.f1: //your implementation break; case R.id.f2: //your implementation break; } } } 
0
source

Define a global credibility similar to this in your work.

 private OnClickLister listener = new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub clearbutton(); ((Button)v).setBackgroundColor(Color.RED); intbtnSelect=v.getTag(); } } 

setTag () data each button in a layout of type 1-2-3 ------ regardless

and set this listener button for everyone.

 f1 =(Button) findViewById(R.id.f1); f1.setTag(1); f2 =(Button) findViewById(R.id.f2); f2.setTag(2); f1.setOnClickListener(listener); f2.setOnClickListener(listener); 

So, when you click the button, intbtnSelect will get the button tag. You do not need the View.OnClickListener tools from your activity.

0
source

Given the official Android documentation: http://developer.android.com/guide/topics/ui/controls/button.html

The method that you declare in the android: onClick attribute must have a signature, as shown above. In particular, the method should:

Publicly

Return void

Define the view as the only parameter (this will click the "View" that was clicked)

So, in XML, mention the method name

 android:onclick="onButtonClicked" 

and in action:

 public void onButtonClicked(View v) { switch (v.getId()) { case R.id.Button1: break; case R.id.Button2: break; . . . . and so on case R.id.Button10: break; } } 
0
source

I have the same problem, but I found a better solution (at least for me) first you need to create a Button list, then int Array id

  Button[] listBtn = new Button[3]; IntId[] idList = {R.id.btn1, R.id.btn2, R.id.btn3}; for(int i = 0 ; i < listBtn.length ; i++) listBtn[i] = (Button) findViewById (idList[i]); for(Button btn:listBtn) btn.setOnClickListener(btnListener); ....... private View.OnClickListener btnListener = new View.OnClickListener() { @Override public void onClick(View v) { for(int i = 0 ; i < listBtn.length ; i++){ if(v.getId() == idArray[i]){ //do something String str = "Button number : "+ i; Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show(); } } } }; 

`

0
source
 public class ExampleActivity extends AppCompatActivity implements View.Onclicklistener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); f1 =(Button) findViewById(R.id.f1); f2 =(Button) findViewById(R.id.f2); Utils.setObjectOnClickListener(this, f1, f2); @Override public void onClick(View view) { Intent intent = null; switch (view.getId()) { case R.id.f1: // Do your code here break; case R.id.f2: // Do your code here break; } } In your Utils.class define this method public static void setObjectOnClickListener(View.OnClickListener listener, View... views) { for (View view : views) { view.setOnClickListener(listener); } } 
0
source

Source: https://habr.com/ru/post/1492304/


All Articles