我需要在tablelayout的一行中获取EditText的值。我动态创建表,行和edittext。我帮助获得子计数,但我似乎仍然无法将用户输入的文本输入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 >
这是生成行内部行和edittexts的代码。
@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;
它让我得到了数,但不是价值。 我不知道每个edittext的id是什么,因此我在创建每个edittext时添加一个标记。但是标签也是动态的,因为它来自文件。
所以,我的问题是我做错了什么以及如何获得每个edittext的值。请注意,表格中可以有10或14个或更多的edittexts。
很抱歉这篇长篇文章,但我希望尽可能完整。 感谢。
好的,这是工作代码。再次感谢
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
}
}
}
}
}
答案 0 :(得分:0)
可能的问题是,您检查TableLayout
的孩子,但您的EditText
和TextView
位于TableRow
,因此您需要检查孩子的孩子。
我认为,就是这样:
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
}
}
}
}
}