テーブルレイアウトの行内に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 >
ここでは、行内に行と編集テキストを生成するコードを示します。
@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がわからないので、それぞれが作成されるときにタグを追加します。しかし、このタグはファイルから来るので動的です。
私の質問は私が間違っていることであり、どのように各編集テキストの価値を得るのですか?あなたには、テーブルに10または14以上のエディットテキストがある可能性があります。
長いポストに申し訳ありませんが、私は可能な限り完全になりたかったです。おかげさまで
さて、ここに作業コードがあります。再び感謝
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
}
}
}
}
}
ありがとうございました。完璧な意味合いを持つ。私はそれを試して、それは素晴らしい動作します。 – vbneil54
私はいくつかの変更を加えなければなりませんでしたが、非常にマイナーでした。上記の作業コードを掲載しました。 – vbneil54
訂正ありがとうございました:) –