2

我不是程序员,我只是喜欢制作对我的工作有帮助的应用程序,所以我对正确编程不太了解。话虽如此,这就是我所反对的。我输入一个 1 到 100 的数字,然后我的应用程序将创建一个包含那么多行的可滚动表格布局。每一行都有一个文本视图、一个编辑文本和另一个文本视图,这是我的代码:

TableLayout tL = (TableLayout)findViewById(R.id.tableLayout1);
    // creates all the fields
    for(int i = 1; i <= numOfInjWells; i++) {
        TableRow tR = new TableRow(this);
        // creates the textView
        TextView tV1 = new TextView(this);
        tV1.setText("      " + i + ":    ");

        // add edit text
        EditText eT = new EditText(this);
        eT.setText("Meter Reading");
        eT.setInputType(InputType.TYPE_CLASS_NUMBER);

        TextView tV2 = new TextView(this);
        tV2.setText("");

        // add the TextView and the editText to the new TableRow
        tR.addView(tV1);
        tR.addView(eT);
        tR.addView(tV2);

        // add the TableRow to the TableLayout
        tL.addView(tR,new TableLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));    
    } // end for statement

所以当我在第一行的edittext(eT)中输入一个数字时,我希望它计算它与我存储在数据库中的数字之间的差异(我已经知道如何从数据库中获取我需要的特定数字)然后更改第一行中的 textview tV2 以便在不单击按钮的情况下显示差异。我遇到的问题是如何将第一行中的edittext(eT)与textview(tV2)相关联,因为所有edittext和textviews都具有相同的名称eT或tV2

感谢您的帮助,很抱歉我对编码知之甚少。

编辑:我考虑过添加

eT.setId(i);
tV2.setId(i); 

但我不知道如何在我的计算中使用它。

4

3 回答 3

0

我遇到的问题是如何将第一行中的edittext(eT)与textview(tV2)相关联,因为所有edittext和textviews都具有相同的名称eT或tV2

一个简单的选项是为这两个设置Views它们之间有关系的 id,当某个特定EditText被修改时,您会发现哪个TextView靠近EditText更新它。例如设置ID:

// add edit text
EditText eT = new EditText(this);
et.setId(1000 + i);
eT.setText("Meter Reading");
eT.setInputType(InputType.TYPE_CLASS_NUMBER);

TextView tV2 = new TextView(this);
tV2.setId(2000 + i); // as you see the difference between the this id and the EditText is 1000
tV2.setText("");

因此,如果您有一个EditText要计算值的引用,您可以简单地通过添加findViewById的 id 来执行 a 。现在,我不知道您是如何根据从那想要的。EditText1000EditTextTextWatcherTextWatcherintEditTextTextView

另外,请注意,如果您创建 50 行或更多行(因为这将导致您的布局中大约有 200 行),Views则每行 3 行(+ 1 TableRow)您可能会遇到应用程序的内存问题。Views可能值得一看ListView

于 2012-07-11T20:47:10.187 回答
0

在查看了 listview 之后,我无法让他们使用 edittext,所以我回到了我的 tablelayout,这是最终的工作版本。

在这里,我创建了字段并将 textwatcher 添加到 edittext 以监视更改:

    TableLayout tL = (TableLayout)findViewById(R.id.tableLayout1);
    // creates all the fields
    for(int i = 1; i <= numOfInjWells; i++) {
        TableRow tR = new TableRow(this);
        // creates the textView
        TextView tV1 = new TextView(this);
        tV1.setText("       " + i + ":    ");

        // add edit text
        eT = new EditText(this);
        eT.setInputType(InputType.TYPE_CLASS_NUMBER);
        eT.setWidth(100);
        eT.setId(1000 + i);
        eT.addTextChangedListener(new CustomTextWatcher(eT));

        tV2 = new TextView(this);
        tV2.setText("");
        tV2.setId(2000 + i);

        // add the TextView and the editText to the new TableRow
        tR.addView(tV1);
        tR.addView(eT);
        tR.addView(tV2);

        // add the TableRow to the TableLayout
        tL.addView(tR,new TableLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));    
    } // end for statement

接下来我为我的 customtextwachter 创建了这个类:

private class CustomTextWatcher implements TextWatcher {
    private EditText mEditText;

    public CustomTextWatcher(EditText eT) {
        mEditText = eT;
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    public void afterTextChanged(Editable s) {
        // gets the change amount for each meter reading from the previous reading
            // this method does the work
        getMeterChange();

    }
} // end class CustomTextWatcher

getMeterChange() 方法完成工作:

public void getMeterChange() {
    int preMeterReading = 0;
    int mReading = 0;

    try {
        cRWLogData.moveToPosition(recordLookUp - 1);
        preMeterReading = cRWLogData.getInt( 14 + rowChanged);
        // finds the edittext that has focus
        View currView = tL.findFocus();
        int currentid = tL.findFocus().getId();
        // gets the string from the edittext and changes it to a int
        EditText currentComponent = (EditText) currView;
        String eTValue = currentComponent.getText().toString();
        mReading = Integer.parseInt(eTValue);
        // calculates difference for what is entered and what is in database
        mChange = mReading - preMeterReading;
        // makes the textview in the same tablerow as the edittext active
        TextView tV2 = (TextView) findViewById(currentid + 1000);
        // sets the text of the textview
        tV2.setText("     " + mChange +"");
    } // end try
    catch (Exception e) {}

} // end getMeterChange
于 2012-07-16T20:40:49.713 回答
0

我知道这是旧的,但对于那些在搜索时发现它的人,我会更新一个更好的选择......

TableLayout tL = (TableLayout)findViewById(R.id.tableLayout1);
// creates all the fields
for(int i = 1; i <= numOfInjWells; i++) {
    TableRow tR = new TableRow(this);
    // creates the textView
    TextView tV1 = new TextView(this);
    tV1.setText("       " + i + ":    ");

    // add edit text
    eT = new EditText(this);
    eT.setInputType(InputType.TYPE_CLASS_NUMBER);
    eT.setWidth(100);


    tV2 = new TextView(this);
    tV2.setText("");
    eT.addTextChangedListener(new CustomTextWatcher(tV2));

    // add the TextView and the editText to the new TableRow
    tR.addView(tV1);
    tR.addView(eT);
    tR.addView(tV2);

    // add the TableRow to the TableLayout
    tL.addView(tR,new TableLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));    
} // end for statement

然后观察者:

private class CustomTextWatcher implements TextWatcher {
    private TextView mTextView;

    public CustomTextWatcher(TextView tV2) {
        mTextView = tV2;
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    public void afterTextChanged(Editable s) {
        // gets the change amount for each meter reading from the previous reading
            // this method does the work

        cRWLogData.moveToPosition(recordLookUp - 1);
        preMeterReading = cRWLogData.getInt( 14 + rowChanged);
        // edit text has just passed the value in Editable s
        mReading = Integer.parseInt(s.toString());
        // calculates difference for what is entered and what is in database
        mChange = mReading - preMeterReading;
        // sets the text of the textview
        mTextView.setText("     " + mChange +"");

    }
} // end class CustomTextWatcher

将值传递给观察者是有原因的。使用框架为您完成硬码。

注意:我实际上并没有编译这个,你可能需要把 try/catch 放回去,为了清楚起见,我只是把它删除了。

于 2013-03-06T12:35:41.367 回答