使用onClick按钮更改EditText的值?

时间:2012-02-22 14:30:08

标签: android function button textview android-edittext

在main.xml中,我创建了一个包含TextView,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

}

1 个答案:

答案 0 :(得分:2)

ID添加Views,以便稍后引用它们。在您的活动中设置3个私有静态int字段( NewValueBox IDNewAddButton的{​​{1}}):

NewMinusButton

然后在您的private static int edt = 1; private static int add = 1001; private static int minus = 2001; 方法中设置ID:

createNewValueBox()

NewValueBox.setId(edt); edt++; NewAddButton

执行相同的操作
NewMinusButton

NewAddButton.setId(add);
add++;

然后在您的监听器中找到按钮,确切地找到单击了哪个添加按钮,并在相应的 NewMinusButton.setId(minus); minus++; 中设置文本:

EditText

一种hackish方法。