첫번째 입력값, 두번째 입력값을 받을 수 있는 Custom View름 만드세요 LinearLayout에 EditText를 두개 놓으신시면 될 것 같구요. 여기에 Textwatcher를 붙이셔서 입력값에 따라 반응하게 하시면 될 것 같구요.
이 커스텀 뷰가 두 입력값에 대한 계산과 결과값을 리턴하도록 하세요.
액티비티나 프레그먼트에는 이 커스텀 뷰를 하나 배치하시고. 버튼을 누를 때마다 이 커스텀뷰를 동적으로 생성하세요. 그리고 화면에 존재하는 커스텀 뷰에 값을 모두 종합해서 화면에 보여주면 됩니다.
안드로이드 개발자 문서에서 커스텀 뷰를 만드는 방법을 먼저 배우세요. 그리고 뷰를 동적으로 생성하는 방법과 뷰와 이벤트 리스너를 연결하는 방법을 배우세요.
대충 코드를 짜보면 이렇게 될 것 같아요.
Custom View:
public class MultiplyView extends FrameLayout {
interface Listener {
void onValueEntered(String s1, String s2);
}
private EditText editText1;
private EditText editText2;
private Listener listener;
public MultiplyView(Context context) {
super(context);
init(null, 0);
}
public MultiplyView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public MultiplyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
LayoutInflater.from(getContext()).inflate(R.layout.view_multiply, this, true);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
TextWatcher editTextListener = new AfterTextChangedListener() {
@Override
public void afterTextChanged(Editable s) {
if (listener != null) {
listener.onValueEntered(getText1(), getText2());
}
}
};
editText1.addTextChangedListener(editTextListener);
editText2.addTextChangedListener(editTextListener);
}
public String getText1() {
return editText1.getText().toString();
}
public String getText2() {
return editText2.getText().toString();
}
public int getValue1() throws NumberFormatException {
return Integer.parseInt(getText1());
}
public int getValue2() throws NumberFormatException {
return Integer.parseInt(getText2());
}
public int getTotal() throws NumberFormatException {
return getValue1() * getValue2();
}
public boolean isEmpty() {
return getText2().isEmpty() && getText2().isEmpty();
}
public boolean isNotEmpty() {
return !isEmpty();
}
public void setListener(Listener listener) {
this.listener = listener;
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements MultiplyView.Listener {
private ViewGroup rootView;
private Button newEditBtn;
private final List<MultiplyView> multiplyViews = new ArrayList<>();
private TextView resultTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
}
private void bindViews() {
rootView = findViewById(R.id.rootView);
resultTxt = findViewById(R.id.resultTxt);
newEditBtn = findViewById(R.id.newEditBtn);
newEditBtn.setOnClickListener(v -> {
MultiplyView view = createMultiplyView();
multiplyViews.add(view);
view.setListener(this);
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private MultiplyView createMultiplyView() {
MultiplyView view = new MultiplyView(this);
rootView.addView(view);
return view;
}
@Override
public void onValueEntered(String s1, String s2) {
try {
int sum = 0;
for (MultiplyView view : multiplyViews) {
if (view.isNotEmpty()) {
sum += view.getTotal();
}
}
resultTxt.setText("Result: " + sum);
} catch (NumberFormatException e) {
// Show error
}
}
}
필요한 내용은 다 포함되었다고 생각합니다. 나머지는 님의 요구사항에 맞춰서 적절하게 배합하시고 수정해 나가시면 되지 않을까 생각합니다.