从函数动态创建的 EditText 中获取文本

Get the text from an EditText created dynamically by a function

我正在使用 Java 开发 Android 应用程序。我正在使用微调器,它为用户提供不同的选项 select 然后我尝试根据 selection 更新 UI,添加更多用户输入以获取更多信息或删除其中的一些。一切正常,直到我调用 saveInfo() 函数,该函数应该在使用 addInput() 函数创建的这个新文本输入字段中打印文本。每次我打印它时,它 returns null ("java.lang.NullPointerException")。有人知道我如何使用函数创建新的 EditText 输入并同时能够从中检索信息吗?如果没有,是否有人会建议将多个 EditText 动态添加到屏幕的另一种解决方案?谢谢!

这是我的代码:

public class UserInfo extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private String[] bankNames={"Teacher","Student","Parent","Worker","Another"};
    //Commum variables:
    private EditText nameInput;
    //Teacher:
    private EditText inSchoolTitleInput;
    //Parent:
    //...

    private EditText neutralInput;
    private String neutralInputText;

    //String from spinner:
    private String selectedName;

    private LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        //Getting the instance of Spinner and applying OnItemSelectedListener on it
        Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
        layout = findViewById(R.id.linearLayout);
        spin.setOnItemSelectedListener(this);

        //Creating the ArrayAdapter instance having the bank name list
        ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        spin.setAdapter(aa);
    }

     @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        selectedName = bankNames[position];
        addFields();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {}

    //Checks what was selected from spinner and updates the screen
    public void addFields() {
        addCommonFields();
        switch (selectedName) {
            case "Teacher": addTeacherFields();
                            break;
            //...
        }
    }

    public void addCommonFields() {
        layout.removeAllViewsInLayout();
        addInput(nameInput, "Lucas Slepetys");
    }

    private void addTeacherFields() {
        addInput(inSchoolTitleInput, "Math");
    }

    public void addInput(EditText field, String hint) {
        neutralInput = field;
        neutralInput = new EditText(this);
        neutralInput.setHint(hint);
        layout.addView(neutralInput);
    }
    
    public void saveInfo(View v) {
        switch (selectedName) {
            case "Teacher":
                System.out.println(neutralInput.getText().toString());
                break;
            //...
        }
     }
}

谢谢!

最好创建一个键值类似于

的 HashMap

HashMap map = new HashMap(); map.put("edit_text_type",editText 对象参考)

因此,每当您创建一个新的动态编辑文本时,也会在此哈希映射中添加,并且当您想要检索特定 HashMap 的值时,您需要从映射中获取相同的键,例如

map.get("edit_text_type").gettext().toString();

下面是参考例子

public void addInput(EditText 字段,字符串提示){

    neutralInput = new EditText(this);
    neutralInput.setHint(hint);
    layout.addView(neutralInput);

   **map.put(hint,field);**
}