0

In Java, if I want to set the value of a JLabel when I click a JButton, I can code it like this...

JButton button = new JButton("add");
JButton label = new JLabel();

public void addListenersToButtons(){
    button.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
    Object src = e.getSource();
    if (src == button) {
        label.setText("this is the number = " + number);
    }
}

I want to be able to do a similar thing in Android, where I set the value of a TextField when the user clicks a button. What is the appropriate code for this in Android?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
user1338675
  • 61
  • 1
  • 3
  • 8

3 Answers3

5

Here's an example:

    final Button btn = (Button) findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView tv = (TextView) findViewById(R.id.TextView1);
            tv.setText("hellooo");
        }
    });
Akhilesh B Chandran
  • 6,523
  • 7
  • 28
  • 55
3
button.setOnCLickListenere(new OnCLickListsner(){

public void onClick(View v){
 text.setText("text"); //assuming you have reference for the TextView and button
}
});

But you definitely need to go through the basics of android first..

ngesh
  • 13,398
  • 4
  • 44
  • 60
0
public static int num=0;
plus.setOnCLickListenere(new OnCLickListsner(){
public void onClick(View v){

num=num+1;
 text.setText("Number "+num); //assuming you have reference for the TextView and button
}
});

minus.setOnCLickListenere(new OnCLickListsner(){
public void onClick(View v){

num=num-1;
 text.setText("Number "+num); //assuming you have reference for the TextView and button
}
});

Hope you want to do something like this. Here are two buttons on pressing plus value will be added, and on pressing minus a number will be deducted.

Bhavin
  • 6,020
  • 4
  • 24
  • 26