0

Ive got this button code as shown below but it only have one button and one textview my project require meant is that there needs to be two or more buttons that will count into separate systems but use the same code. Ill include the main code below but any help adding more functionality into the system is much loved!

main code

package com.example.counter;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
    // Private member field to keep track of the count
    private static int mCount = 0;

    private TextView countTextView;
    private Button countButton;
    public static final String PREFS_NAME = "com.example.myApp.mCount";
    private SharedPreferences settings = null;
    private SharedPreferences.Editor editor = null;

    /** ADD THIS METHOD **/
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.main);
      countTextView = (TextView) findViewById(R.id.TextViewCount);
      countButton = (Button) findViewById(R.id.ButtonCount);

      countButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              mCount++;
              countTextView.setText("Count: " + mCount);
              editor = settings.edit(); 
              editor.putInt("mCount", mCount);
              editor.commit();
          }
      });
    settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);


     }

    @Override
    public void onPause() {
      super.onPause();  
    }

    @Override
    public void onResume() {
      super.onResume();  
      mCount = settings.getInt("mCount", 0);
      countTextView.setText("Count: " + mCount);
    }
    }
James
  • 59
  • 7
  • So add the extra buttons you need. And if you really needed to ask a question for something that simple, read some Android tutorials. – Gabe Sechan Feb 19 '13 at 20:00
  • not that simple ive tried adding buttons and it doesnt work even when i change the code – James Feb 19 '13 at 20:03
  • I am not entirely sure of your question, do you need to have 3 buttons each with the same onClickListener, a button in 3 different activities with the same onClickListener, or something entirely different? – Brent Hronik Feb 19 '13 at 20:05
  • the 3 buttons need to run on the same kinda code but will each count differently - you can press one without it affecting the others – James Feb 19 '13 at 20:07
  • I am not entirely sure what is meant by "same kinda code but will each count differently", but I believe you want 3 buttons with different onClickListeners? ie if I click button 1 something should happen, if I click button 2 something different should happend, and button 3 should do something different yet? – Brent Hronik Feb 19 '13 at 20:10
  • yeah thats what i mean sorry :p – James Feb 19 '13 at 20:11
  • Ok then I have posted some sample code below that should give you the right idea of what you need to accomplish. – Brent Hronik Feb 19 '13 at 20:18

1 Answers1

0

Create 3 buttons in your xml, lets assume their ids are ButtonCount, ButtonCount2 and ButtonCount3 as well as the countButton2 and countButton3 being declared as well. Then initialize them as follows:

  countButton = (Button) findViewById(R.id.ButtonCount);

  countButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          mCount++;
          countTextView.setText("Count: " + mCount);
          editor = settings.edit(); 
          editor.putInt("mCount", mCount);
          editor.commit();
      }
  });

  countButton2 = (Button) findViewById(R.id.ButtonCount2);

  countButton2.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          //do something here
      }
  });

  countButton3 = (Button) findViewById(R.id.ButtonCount3);

  countButton3.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          //do something else here
      }
  });
Brent Hronik
  • 2,357
  • 1
  • 27
  • 43