gpt4 book ai didi

java - 安卓工作室 : How to programmatically change an adjacent TextView on the same tablerow?

转载 作者:行者123 更新时间:2023-11-29 23:38:07 26 4
gpt4 key购买 nike

这是一个库存盘点应用程序...加载库存数据库中的项目数量,库存审计员必须输入每个项目的盘点数量,应用程序会告诉他们它是否正确。:
< br/>1) 应用程序从服务器接收一个包含商品列表及其库存数量的 JSON 字符串。它被加载到名为“jArray”的数组中

2) 从此数组中,UI 在新行中显示每个项目:
第 1 列:项目描述,
第 2 列:用于用户输入盘点数量的编辑文本字段,
第 3 列:包含正确数量的隐藏(白色背景中的白色文本)字段..
< br/>3) 如果输入的数量与 col 3 中的文本不匹配,那么它会将 col3 textview 颜色更改为红色作为警告...

问题是,我如何在同一表格行上引用/检查以编程方式创建的相邻 TextView 的值,并更改其文本颜色?

在带有 javascript 的 html 网站上,这只是引用同一父节点下的第 3 列子节点的问题……但是在 android/java 中有什么等价物……或者有更简单的方法吗?

谢谢

                for (int i=0; i < jArray.length(); i++)
{
try {
JSONObject oneObject = jArray.getJSONObject(i);

// Pulling items from the array
item = oneObject.getString("item");
qty = oneObject.getString("qty");

// textview on column 1 containing item descr
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.1f));
t1v.setText(item);
tbrow.addView(t1v);

// Add edit text field for qty input
EditText t4v = new EditText(this);
t4v.setInputType(InputType.TYPE_CLASS_NUMBER);
t4v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.1f));
tbrow.addView(t4v);
t4v.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence https://stackoverflow.com/editing-helpcharSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// after input from android keypad, check whether the number matches with qty in adjacent textview, if not matching, change adjacent textview color to red
if(s != ***value in adjacent textview *****) {
t5v.setTextColor(Color.RED);}
}

@Override
public void afterTextChanged(Editable editable) {

}
});

// Add invisible textview (text in white) holding actual quantity of item
TextView t5v = new TextView(this);
t5v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.1f));
t5v.setText(qty);
t5v.setTextColor(Color.WHITE);

最佳答案

当使用 View.setTag(Object) 将其添加到表格行时,您可以在 TextView 上设置标签。例如使用一些

private static final String QUANTITY_VIEW = "quantity";

t5v.setTag(QUANTITY_TAG);
tbrow.add(t5v);

然后你可以通过写来检索它

TextView textView = (TextView) tbrow.findViewWithTag(QUANTITY_TAG);

另请参阅 View.findViewWithTag() 的文档

关于java - 安卓工作室 : How to programmatically change an adjacent TextView on the same tablerow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52083735/

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