简体   繁体   English

android studio:如何以编程方式更改同一tablerow 上的相邻TextView?

[英]android studio: How to programmatically change an adjacent TextView on the same tablerow?

It's an inventory count app...that loads the quantity of items on the inventory database and the inventory auditor has to enter the counted quantity for each item and the app will tell them whether it's correct.:这是一个库存盘点应用程序...加载库存数据库中的项目数量,库存审核员必须输入每个项目的盘点数量,应用程序将告诉他们是否正确。:

1) the app receives a JSON string from the server containing a list of items and their stock quantity..it's loaded into an array named "jArray" 1) 应用程序从服务器接收一个包含项目列表及其库存数量的 JSON 字符串。它被加载到一个名为“jArray”的数组中

2) from this Array, the UI display each item in a new row: 2) 从这个 Array 中,UI 在新行中显示每个项目:
col 1: item description,第 1 列:项目描述,
col 2: edittext field for user input of counted quantity,第 2 列:用于用户输入计数数量的编辑文本字段,
col 3: a hidden (white text in white bg) field containing the correct quantity..第 3 列:包含正确数量的隐藏(白色背景中的白色文本)字段。

3) if the entered qty does not match the text in col 3, then it will chg the col3 textview color to red as a warning... 3) 如果输入的数量与第 3 列中的文本不匹配,那么它会将 col3 文本视图颜色更改为红色作为警告...

issue is, how do i reference/check the value on the adjacent programmatically created textview on the same tablerow and also change its text color?问题是,我如何引用/检查同一 tablerow 上以编程方式创建的相邻 textview 上的值并更改其文本颜色?

on an html website with javascript, it's just a matter of referencing the 3rd column childnode under the same parentnode ...but what is the equivalent in android/java..or is there an easier way to do this?在带有 javascript 的 html 网站上,这只是在同一父节点下引用第 3 列子节点的问题……但是 android/java 中的等价物是什么?

thx谢谢

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

You can set a tag on the TextView when adding it to the table row by using View.setTag(Object) .使用View.setTag(Object)TextView添加到表格行时,您可以在TextView上设置标签。 For example use some例如使用一些

private static final String QUANTITY_VIEW = "quantity";

and write和写

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

Then you can retrieve it by writing然后你可以通过写来检索它

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

See also the docs for View.findViewWithTag()另请参阅View.findViewWithTag()的文档

You can reference any view by assigning it a custom id, and then pulling it via that ID from its parent.您可以通过为其分配一个自定义 ID 来引用任何视图,然后通过该 ID 从其父视图中提取它。

Example:例子:

// r.id.rootlayout is just an arbitrary ID. Replace rootlayout with whatever the ID is of your parent view
ViewGroup parent = findViewById(r.id.rootlayout);

// viewToBeEditedID is the ID of the view you wish to edit. This can be found in any number of ways, but you will be best to track it since it seems that this is dynamically being done.     
TextView viewToBeEdited = (TextView) parent.findViewById(viewToBeEditedID);

// Updated Text is whatever you wish to display.
viewToBeEdited.setText(updatedText);

// Invalidate will tell the parent to "refresh" and show any changes made. Not always necessary, but good practice
parent.invalidate();

The trick will be to figure out a clever method of keeping track of your view ID's without being able to actually know them.诀窍是找出一种聪明的方法来跟踪您的视图 ID,而实际上无法知道它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式更改 textView 的颜色以在 Android Studio 上形成良好的对比? - How to change the color of a textView programmatically for a good contrast on Android Studio? 如何在Android中更改TableRow高度? - How to change TableRow height in Android? 如何从Android Studio上的片段更改TextView - How change a textview from a fragment on android studio 将TextView设置为TableRow,IllegalStateException-Android - Setting TextView to a TableRow, IllegalStateException - Android 如何使用Android将动态TableRow放置在TableLayout中并将Textview动态放置在TableRow中? - how to put dynamic TableRow in TableLayout and place Textview dynamically in TableRow using Android? 如何在Android Studio上以编程方式更改小部件ID - how to change widget id programmatically on android studio Android studio - 在 textView 中更改字符串 - Android studio - Change string in textView Android-以编程方式对TableRow列进行对齐 - Android - TableRow column alignment programmatically android自动横向滚动在TableLayout的TableRow中的TextView? - android automatic horizontal scrolling TextView in a TableRow in a TableLayout? Android的Textview和TableRow权重无法正常工作 - Textview and TableRow Weights Not Working Properly Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM