2012-04-17 33 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一個數字將被扣除。