editText.getText().toString()无法输入数据

editText.getText().toString() can't enter data

我正在尝试制作一个可以添加科目和成绩的应用程序,目前,我正在开发一个用于添加科目的系统。所以,我有一个按钮,当按下它时,它会生成一个 editText“字段”,您可以在其中输入主题的名称。问题是,当您输入一些文本并按 ENTER 键时,它会在“字段”中创建一个新行。它不会“处理”我写的文本,它只是换行。

有人知道解决这个问题的方法吗?谢谢!

问题图片:https://i.stack.imgur.com/HiXjM.png

这是按下按钮时运行的代码:

        EditText editSubjectName = new EditText(this);
        editSubjectName.setHint("Enter subject name");
        linearLayout.addView(editSubjectName);
        String name = editSubjectName.getText().toString();

        TextView subjectName = new TextView(this);
        subjectName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        subjectName.setText(name);
        linearLayout.addView(subjectName);

请使用以下示例

EditText editSubjectName = new EditText(this);
editSubjectName.setHint("Enter subject name");
editSubjectName.setSingleLine(true);
linearLayout.addView(editSubjectName);


TextView subjectName = new TextView(this);
subjectName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.addView(subjectName);


editSubjectName.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Perform action on key press
           String name = editSubjectName.getText().toString();
           subjectName.setText(name);
          return true;
        }
        return false;
    }
});