jeudi 22 janvier 2015

Creating Methods in Separate Classes


Vote count:

0




I was wondering if anyone had any idea how to create a method within a separate class, and have it be able to change int values in the main activity. If you look at this simple counter below, I want to be able to put the counter +=1 code etc in the onClicks, within a method in a separate class. Any help would be much appreciated.


public class MainActivity extends ActionBarActivity {



int counter = 0;
TextView counterDisplay;
Button plusOne, minusOne;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// Plus One Button



plusOne = (Button) findViewById(R.id.btnPlus);
counterDisplay = (TextView) findViewById(R.id.tv);


plusOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

counter += 1;
counterDisplay.setText("" + counter);
}
});


// Subtract One Button



minusOne = (Button) findViewById(R.id.btnMinus);
counterDisplay = (TextView) findViewById(R.id.tv);

minusOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

counter -= 1;
counterDisplay.setText("" + counter);
}
});

}


}



asked 1 min ago







Creating Methods in Separate Classes

Aucun commentaire:

Enregistrer un commentaire