如何在android中创建和处理这种类型的事件。这张图片取自ios,但我想在android中实现它。
提前致谢。抱歉英语不好。
答案 0 :(得分:0)
请按照以下步骤操作:
1) take hidden Layout.
2) Make one layout that include your Button , Editext.. etc.
3) When Click on Plus Button inflate layout & add it to hidden layout.
使用此代码:
LayoutInflater inflater = LayoutInflater.from(context);
View mView = (View) inflater.inflate(R.layout.custom_layout, null, false);
// Access button & Edittext like:
Button mButton= (Button)mView.findViewById(R.id.mButton);
EditText mEdiText= (EditText)mView.findViewById(R.id.mEditText);
LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);
hiddenLinear.addView(mView);
修改强>
添加多个EditText使用 FOR 循环
LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);
for (int i = 0, l <= 3; i++)
{
LayoutInflater inflater = LayoutInflater.from(context);
View mView = (View) inflater.inflate(R.layout.custom_layout, null, false);
// Access button & Edittext like:
Button mButton= (Button)mView.findViewById(R.id.mButton);
EditText mEdiText= (EditText)mView.findViewById(R.id.mEditText);
hiddenLinear.addView(mView);
}
&安培;从All View获取所有EditText值使用此方法:
LinearLayout hiddenLinear = (LinearLayout)findViewById(R.id.myLayout);
findAllEdittexts(hiddenLinear);
ArrayList<EditText> array = new ArrayList<>();
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof EditText) {
EditText edittext = (EditText) view;
array.add(edittext);
}
}
}