2013-07-02 49 views
0

當我想上下班2 TextView的A和B,我做的:如何來來回回兩個按鈕

string temp = tvA.getText().toString(); 
tvA.setText(tvB.getText().toString()); 
tvB.setText(temp); 

所以,如果我要上班2鍵,怎麼樣?

+0

你究竟想要做什麼?交換按鈕上的標籤?你在用什麼語言? –

+0

我在eclipse中使用Java。當我點擊更改按鈕時,我想要按鈕A變成按鈕B,按鈕B變成按鈕A.請幫助我。 –

+0

我還不確定你想要做什麼。你想交換按鈕上的文字或移動按鈕和交換位置嗎?你使用awt,Swing還是其他的東西? –

回答

1

通勤是一個正確的說法,但它使你想什麼來實現聲音更加困難比它確實是...

整體思路,這從你的代碼很明顯,大約是交換標籤。你必須一個ActionListener添加到每個按鈕:

jButton1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

,接着讓每個ActionListener的通話commute();

沒有必要使用toString()因爲getText()返回一個字符串了。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    commute(); 
}           

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           
    commute(); 
}           

private void commute() { 
    String temp = jButton1.getText(); 
    jButton1.setText(jButton2.getText()); 
    jButton2.setText(temp); 
}