如何获取表格布局一行中内容的值

How do I get the value of the contents inside a row of a tablelayout

我需要在 table 布局的一行中获取 EditText 的值。我动态创建 table、行和编辑文本。我在获取 childcount 方面得到了帮助,但我似乎仍然无法获取用户输入到 edittext 中的文本。这是创建基本布局的 xml 代码。

<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
            xmlns:tools = "http://schemas.android.com/tools"
            android:layout_width = "match_parent"
            android:layout_height = "match_parent"
            tools:context = "com.example.neil.hvacbuilder.MainActivity" >
        <ScrollView
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:id = "@+id/scrollView"
    android:layout_alignParentLeft = "true"
    android:layout_alignParentStart = "true"
    android:layout_below = "@+id/imgPartPicked" >

    <TableLayout
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:stretchColumns="*"
        android:id = "@+id/tblLayoutContent"
        android:layout_alignParentLeft = "true"
        android:layout_alignParentStart = "true"
        android:layout_alignParentBottom = "true" >
    </TableLayout >
</ScrollView >
</RelativeLayout >

这是生成行和行内编辑文本的代码。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.part_detail);
    Bundle bundle = getIntent().getExtras();
    String btnName = bundle.getString("btnNameStored");
    String btnOrig = bundle.getString("btnOrig");

    TextView textView = (TextView) findViewById(R.id.txtBtnPushed);
    textView.setText(btnOrig);
    BufferedReader reader;
    InputStream is = null;

    // Get the name of the part picked and then grab the dimensions that are needed for that
    // part.

    try {

        is = getAssets().open(btnName);

        reader = new BufferedReader(new InputStreamReader(is));

        String line = reader.readLine();
        int lineLength = (line.length());

        TableLayout table = (TableLayout) findViewById(R.id.tblLayoutContent);

        while (line != null){

            TableRow tblRow = new TableRow(this);
            tblRow.setPadding(5, 30, 5, 5);
            table.addView(tblRow);
            line = line.toUpperCase();

            // sets the max number of columns to 2 and iterates through the number of lines.
            // filling each cell with a Text Box with the name of each dimension of the part
            // that was picked. And a EditView for the user to put in the dimensions.

            for (int col = 0; col < NUM_COL; col++) {
                //This is the label of what measurement needs to be enter.
                TextView lblName = new TextView(this);
                // This is the number you enter for the measurement.
                EditText txtPartMeasurement = new EditText(this);

                txtPartMeasurement.setInputType(InputType.TYPE_CLASS_NUMBER |
                        InputType.TYPE_NUMBER_FLAG_DECIMAL);

                // Set all the input attributes for the text boxes.

                txtPartMeasurement.setTextSize(14);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    txtPartMeasurement.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
                }
                txtPartMeasurement.setEnabled(true);

                // Set all the input attributes for the labels of what needs to be entered.
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    lblName.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                }
                lblName.setBackgroundColor(getResources().getColor(R.color.colorPartMeasurements));
                lblName.setFocusable(true);
                lblName.setText(line);
                lblName.setTextSize(14);

                txtPartMeasurement.setTag(line);

                // Add the labels and text boxes to the grid.
                tblRow.addView(lblName);
                tblRow.addView(txtPartMeasurement);

                // Get the next line in the file if there is one.
                line = reader.readLine();
            }
        };

    }
    catch (IOException e) {
        e.printStackTrace();
    }

}

这是为我提供用户输入的值的代码,但它不能正常工作。

    @Override
public void onClick(View v) {
    Button button = (Button) v;
    String btnText = button.getText().toString();


    switch (v.getId()) {
        //This is the Save and Continue button. It saves the info entered and goes back to
        // pick the next part needed.
        case R.id.btnNextPartDetail:

            TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent));
            int childParts = PartDetailLayout.getChildCount();
            if (PartDetailLayout != null) {
                for (int i = 0; i < PartDetailLayout.getChildCount(); i++) {
                    View viewChild = PartDetailLayout.getChildAt(i);
                    if (viewChild instanceof EditText) {
                        // get text from edit text
                        String text = ((EditText) viewChild).getText().toString();

                    }
                    else if (viewChild instanceof TextView) {
                        // get text from text view
                        String text = ((TextView) viewChild).getText().toString();
                        //TODO: add rest of the logic
                    }
                }
            }

            Toast.makeText(getApplicationContext(),
                    "Button Save/Continue Selected",
                    Toast.LENGTH_LONG).show();
            break;

它让我得到了计数,但没有得到值。 我不知道每个编辑文本的 ID 是什么,所以我在创建每个编辑文本时添加了一个标签。但是标签也是动态的,因为它来自文件。

所以,我的问题是我做错了什么以及如何获取每个编辑文本的值。请注意,table.

中可以有 10 个或 14 个或更多的编辑文本

很抱歉 post,但我希望尽可能完整。 谢谢

好的,这是工作代码。再次感谢

                TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent));
            int childParts = PartDetailLayout.getChildCount();
            if (PartDetailLayout != null) {
                for (int i = 0; i < childParts; i++) {
                    View viewChild = PartDetailLayout.getChildAt(i);
                    if (viewChild instanceof TableRow) {
                        int rowChildParts = ((TableRow) viewChild).getChildCount();
                        for (int j = 0; j < rowChildParts; j++) {
                            View viewChild2 = ((TableRow) viewChild).getChildAt(j);
                            if (viewChild2 instanceof EditText) {
                                // get text from edit text
                                String txtEdit = ((EditText) viewChild2).getText()
                                        .toString();

                            } else if (viewChild2 instanceof TextView) {
                                // get text from text view
                                String txttext = ((TextView) viewChild2).getText().toString();

                                //TODO: add rest of the logic
                            }
                        }
                    }
                }
            }

可能的问题是您检查了 TableLayout 的孩子,但是您的 EditTextTextViewTableRow 中,所以您需要检查孩子的孩子。

我想,应该是这样的:

TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent));
        iTableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent));
int childParts = PartDetailLayout.getChildCount();
if (PartDetailLayout != null) {
    for (int i = 0; i < childParts; i++) {
        View viewChild = PartDetailLayout.getChildAt(i);
        if (viewChild instanceof TableRow) {
            int rowChildParts = ((TableRow) viewChild).getChildCount();
            for (int j = 0; j < rowChildParts; j++) {
                View viewChild2 = ((TableRow) viewChild).getChildAt(j);
                if (viewChild2 instanceof EditText) {
                    // get text from edit text
                    String text = ((EditText) viewChild2).getText().toString();
                } else if (viewChild2 instanceof TextView) {
                    // get text from text view
                    String text = ((TextView) viewChild2).getText().toString();
                    //TODO: add rest of the logic
                }
            }
        }
    }
}