2012-02-22 148 views
1

在main.xml中,我創建了一個包含TextView,EditText和「+」和「 - 」按鈕的行。使用onClick按鈕更改EditText的值?

下面,我做了一個「添加」按鈕,將幫助您創建一個新的行當你點擊添加按鈕,你會得到一個EditText和一個提交和取消按鈕。

在「提交」上,它將EditText值輸出到TextView並創建與第一個相同的行。

當按下「+」按鈕時,數值「NewValueBox」應爲+1。 但是因爲我在另一個函數中調用它,所以無法通過createNewAddButton()函數來識別按鈕的設置。

簡而言之: 「當我點擊NewAddButton時,如何更改NewValueBox的值?

下面的代碼:

package com.lars.MyApp; 

import com.google.ads.*; 
import com.lars.MyApp.R; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.text.InputType; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class DrinkRecOrderActivity extends Activity { 

    int currentValue1 = 0; 
    int currentValueNew = 0; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final EditText firstValue = (EditText) findViewById(R.id.firstValue); 
     Button valuePlus = (Button) findViewById(R.id.valuePlus); 
     Button valueMinus = (Button) findViewById(R.id.valueMinus); 

     final Button addValue = (Button) findViewById(R.id.add); 

     final TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1); 
     final LinearLayout addValueRow = (LinearLayout) findViewById(R.id.addValueRow); 

     final EditText addNewValue = (EditText) findViewById(R.id.addNewValue); 
     final Button submitNewValue = (Button) findViewById(R.id.submitNewValue); 
     final Button cancelNewValue = (Button) findViewById(R.id.cancelNewValue); 

     // BEGIN ONCLICKLISTENERS 
     valuePlus.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       plusValue(); 
       firstValue.setText("" + currentValue1); 
      } 
     }); 

     valueMinus.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       minValue(); 
       firstValue.setText("" + currentValue1); 
      } 
     });  

     addValue.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       addValueRow.setVisibility(View.VISIBLE); 
       addValue.setVisibility(View.GONE); 
      } 
     }); 

     cancelNewValue.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       addValueRow.setVisibility(View.GONE); 
       addValue.setVisibility(View.VISIBLE); 
      } 
     }); 

     submitNewValue.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       tableLayout1.addView(createnewRow()); 
       addValueRow.setVisibility(View.GONE); 
       addValue.setVisibility(View.VISIBLE); 
       addNewValue.setText(""); 
      } 
     }); 

     // END ONCLICKLISTENERS 

     // Look up the AdView as a resource and load a request. 
     AdView adView = (AdView) this.findViewById(R.id.adView); 
     adView.loadAd(new AdRequest()); 
    } 

    public TableRow createNewRow() { 
     final TableRow newRow = new TableRow(this); 
     final EditText addNewValue = (EditText) findViewById(R.id.addNewValue); 
     newRow.addView(createNewTextView(addNewValue.getText().toString())); 
     newRow.addView(createNewValueBox()); 
     newRow.addView(createNewAddButton()); 
     newRow.addView(createNewMinusButton()); 
     return newRow; 
    } 

    public TextView createNewTextView(String text) { 
     final TextView textView = new TextView(this); 
     textView.setText(text); 
     return textView; 
    } 

    public EditText createNewValueBox() { 
     EditText NewValueBox = new EditText(this); 
     NewValueBox.setHint("0"); 
     NewValueBox.setInputType(InputType.TYPE_CLASS_NUMBER); 
     return NewValueBox; 
    } 

    public Button createNewAddButton() { 
     final Button NewAddButton = new Button(this); 

     NewAddButton.setText("+"); 

     NewAddButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       plusNew(); 

       //NewValueBox.setText("" + currentValueNew); 
      } 

     }); 

     return NewAddButton; 
    } 

    public Button createNewMinusButton() { 
     final Button NewMinusButton = new Button(this); 
     NewMinusButton.setText("-"); 
     return NewMinusButton; 
    } 

    // BEGIN PLUS AND MIN FUNCTIONS 

    public void plusNew() { 
     if (currentValueNew <= 999) { 
      currentValueNew = currentValueNew + 1; 
     } 
    } 

    public void plusValue() { 
     if (currentValue1 <= 999) { 
      currentValue1 = currentValue1 + 1; 
     } 
    } 

    public void minValue() { 
     if (currentValue1 >= 1) { 
      currentValue1 = currentValue1 - 1; 
     } 
    } 

    // END PLUS AND MIN FUNCTIONS 

} 
+0

在按鈕的on-click-handler函數中,在特定的EditText對象上調用setText('chars')。 – hovanessyan 2012-02-22 14:37:53

回答

2

添加ID爲了你們的Views,所以你可以在以後引用它們。讓您的活動3私有靜態詮釋字段(IDNewValueBoxNewAddButtonNewMinusButton):

private static int edt = 1; 
private static int add = 1001; 
private static int minus = 2001; 

然後在您的createNewValueBox()方法設置ID:

NewValueBox.setId(edt); 
edt++; 

做的一樣NewAddButtonNewMinusButton

NewAddButton.setId(add); 
add++; 

NewMinusButton.setId(minus); 
    minus++; 

然後在你的聽衆的按鈕找出到底是哪個添加按鈕被點擊,並設置相應的EditText文:

NewAddButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       plusNew(); 
       int tmp = v.getId(); 
       EditText temp = (EditText) findViewById(1 + (tmp - 1001)); 
       temp.setText("" + currentValueNew); 
      } 

類hackish的方法。

+0

非常感謝!這工作! 只有我現在得到了...當我添加多行,例如我按「+」3次。然後按其他值,一旦它變爲4. 所以currentValueNew正在...... – 2012-02-22 19:43:33

+0

@LarsVandeDonk你應該有一個單獨的數組來保存新值,因爲你正在與你認爲所添加的所有行共享該字段。 – Luksprog 2012-02-22 20:00:24

+0

是的,我想,有沒有辦法做同樣的事情,你在v.getId();數組?所以我計算數組的數量或者什麼? – 2012-02-22 20:08:43