im试图通过(+)按钮获取动态添加的edittext字段的值,并将其存储在变量中,然后对其进行一些操作。添加edittext字段没有问题。我是Java和android的新手,请原谅我是否太简单了,无法向您询问,但是找不到经验丰富的人提供的更好的地方,可以给我我可以理解的答案。同样,如果只有一个edittext字段,我可以通过元素的@id来实现,但是由于这些是通过按钮动态添加的字段,并且都具有相同的名称,因此有点难以解决。 所以这是场景:我的应用程序中有一个按钮,通过按下它会添加一个线性布局,其中包括edittext字段,一个下拉框和一个remove按钮。我想放另一个键来查找那里有多少edittext字段,获取它们中的字符串值,将它们转换为double值,将它们全部求和,然后将它们存储在double变量中。 谢谢您的帮助。
activity_main.xml
package com.example.dell.cheapin;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
public LinearLayout parentLinearLayout;
public ConstraintLayout FieldLayout;
public static final String Computable_Edit_text_tag="EditTextTag";
public double totalvalue, textvalue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field,parentLinearLayout,false);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
public void onCompute(View v){
int size = parentLinearLayout.getChildCount();
for (int i=0; i <= size; i++) {
View FieldView = parentLinearLayout.getChildAt(i);
FieldLayout = (ConstraintLayout)findViewById(R.id.field_layout);
int size2 = FieldLayout.getChildCount();
for (int j = 0; j <= size2; j++) {
View FieldView2 = FieldLayout.getChildAt(j);
if (FieldView2 instanceof EditText && Computable_Edit_text_tag.equals(FieldView2.getTag().toString())) {
EditText et = (EditText) FieldView2;
totalvalue = Integer.parseInt(et.toString()) + totalvalue;
}
}
}
Snackbar TotalSnackbar = Snackbar.make(parentLinearLayout,Double.toString(totalvalue),Snackbar.LENGTH_LONG);
TotalSnackbar.show();
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
public void onEnter(View v){
EditText ET = (EditText)findViewById(R.id.amount_edit_text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MainActivity.java
FieldLayout=fieldView;
好吧,我能够在您的帮助下解决此问题,并且还必须在int size2 = FieldLayout.getChildCount();
之前添加FieldLayout
,因为如果没有Null
会返回public void onCompute(View v){
totalvalue=0;
int size = parentLinearLayout.getChildCount();
for (int i=0; i < size; i++) {
ConstraintLayout fieldView = (ConstraintLayout)parentLinearLayout.getChildAt(i);
FieldLayout=fieldView;
int size2 = FieldLayout.getChildCount();
for (int j = 0; j < size2; j++) {
View FieldView2 = FieldLayout.getChildAt(j);
if (FieldView2 instanceof EditText && Computable_Edit_text_tag.equals(FieldView2.getTag().toString())) {
EditText et = (EditText) FieldView2;
String myNumber = et.getText().toString();
totalvalue = Integer.parseInt(myNumber) + totalvalue;
}
}
}
Snackbar TotalSnackbar = Snackbar.make(parentLinearLayout,getString(R.string.TotalMessage)+" "+String.valueOf(totalvalue),Snackbar.LENGTH_LONG);
TotalSnackbar.show();
,并且应用程序将崩溃。
这是完成的代码:
(Get-AzureKeyVaultSecret -vaultName "debugkv" -name "CoreConfig-StorageAccount-AccessKey")
答案 0 :(得分:0)
每次将EditText
添加到ViewGroup
中时,就对其进行标记。有一种方法View#setTag(Object)
就是这样做的。例如使用一些private static final String EDIT_TEXT_TAG="edittexttag";
要计算总和,您可以循环遍历EditText
中的所有ViewGroup
(假设它由变量myViewGroup
引用)
int size = myViewGroup.getChildCount();
for(int i = 0; i < size; i++){
View view = myViewGroup.getChildAt(i);
if(view instanceof EditText && EDIT_TEXT_TAG.equals(view.getTag().toString())){
EditText et = (EditText)view;
// get the number and add it to the total...
}
}
请注意,getChildAt()
仅会给您直接的孩子。如果EditText
是其他子项ViewGroup
的一部分,那么您将不得不调整逻辑以在该子项ViewGroup
中寻找它们。但是,如果EditText
中只有一个带有特定标签的ViewGroup
,则不必遍历整个子树即可获得它,但是可以使用View#findViewWithTag()
-这将给您是第一个(大)*子View
,它恰巧找到了所需标签。
我将添加您的onCompute()
版本以显示我的意思
public void onCompute(View v){
int size = parentLinearLayout.getChildCount();
// "<" not "<=" !
// (counting starts at 1, index starts at 0, number of steps must be the same)
for (int i=0; i < size; i++) {
// the ConstraintLayouts are all direct
// children of the parent LinearLayout
ConstraintLayout fieldView = (ConstraintLayout)parentLinearLayout.getChildAt(i);
int size2 = fieldView.getChildCount();
// Again: "<" not "<=" !
for (int j = 0; j < size2; j++) {
View fieldView2 = fieldView.getChildAt(j);
if (fieldView2 instanceof EditText && Computable_Edit_text_tag.equals(fieldView2.getTag().toString())) {
EditText et = (EditText) fieldView2;
// not et.toString() but et.getText().toString()
//
String myNumber = et.getText().toString();
totalvalue = Integer.parseInt(myNumber) + totalvalue;
}
}
}
Snackbar TotalSnackbar = Snackbar.make(parentLinearLayout,Double.toString(totalvalue),Snackbar.LENGTH_LONG );
TotalSnackbar.show();
}
请注意,如果用户设法输入除数字以外的任何内容,这将会崩溃-但是如何避免使用NumberFormatException
,这是另一个问题