gpt4 book ai didi

android - 使用 onClick 按钮更改 EditText 的值?

转载 作者:行者123 更新时间:2023-11-29 22:12:01 25 4
gpt4 key购买 nike

在 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

}

最佳答案

为您的Views 添加ID,以便您以后可以引用它们。在您的 Activity 中创建 3 个私有(private)静态 int 字段(NewValueBoxIDNewAddButtonNewMinusButton):

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

一种骇人听闻的方法。

关于android - 使用 onClick 按钮更改 EditText 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9396727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com