【发布时间】:2022-01-04 14:29:14
【问题描述】:
我有这个代码:
nameInputLayout.requestFocus();
argentInputLayout.getEditText().setOnEditorActionListener((v, actionId, event) -> {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
addNewFields(this,layoutNewDepenses);
handled = true;
View lastChargeView = layoutNewDepenses.getChildAt(layoutNewDepenses.getChildCount() - 1);
TextInputLayout nameInputLayout = lastChargeView.findViewById(R.id.nom);
nameInputLayout.requestFocus();
TextInputLayout argentInputLayout = lastChargeView.findViewById(R.id.argent);
argentInputLayout.getEditText().setOnEditorActionListener((v2, actionId2, event2) -> {
boolean handled2 = false;
if (actionId2 == EditorInfo.IME_ACTION_DONE) {
addNewFields(this,layoutNewDepenses);
handled2 = true;
View lastChargeView2 = layoutNewDepenses.getChildAt(layoutNewDepenses.getChildCount() - 1);
TextInputLayout nameInputLayout2 = lastChargeView2.findViewById(R.id.nom);
nameInputLayout2.requestFocus();
TextInputLayout argentInputLayout2 = lastChargeView2.findViewById(R.id.argent);
argentInputLayout2.getEditText().setOnEditorActionListener((v3, actionId3, event3) -> {
boolean handled3 = false;
if (actionId3 == EditorInfo.IME_ACTION_DONE) {
addNewFields(this,layoutNewDepenses);
handled3 = true;
}
return handled3;
});
}
return handled2;
});
}
return handled;
});
它工作得很好,但它非常重复。我想要做的是,每当用户在 argentInputLayout 字段中完成写入并单击回车按钮时,必须再添加两个 TextInputLayout 字段。
问题是我不知道用户需要多少字段,并且我不想像现在那样做,因为这将是太多的代码行。那么任何人都可以帮助找到我怎样才能使它更简单?
【问题讨论】:
标签: java android performance android-studio