2012-04-17 32 views
0

在Java中,如果我想设置一个JLabel的值当我点击一个JButton,我可以这样的代码吧...设置一个Android的TextView的价值,当我点击一个按钮

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); 
    } 
} 

我希望能够在Android中做类似的事情,当用户点击一个按钮时,我设置了一个TextField的值。 Android中这个适当的代码是什么?

+5

必须在互联网上有10k个例子...... – 2012-04-17 12:35:12

+0

必须要研究最少5-10分钟的问题。 – 2012-04-17 13:01:25

回答

3
button.setOnCLickListenere(new OnCLickListsner(){ 

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

但你绝对需要经过的Android首次基础..

5

下面是一个例子:

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"); 
     } 
    }); 
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 
} 
}); 

希望你想要做这样的事情。这里是按下plus两个按钮的值将被添加,并按下minus一个数字将被扣除。