添加/删除编辑文本和点击删除(X)
我是新来的android开发。添加/删除编辑文本和点击删除(X)
我想添加EditText和删除按钮(X)时,他们点击一个按钮(称为“联系”)。这样就会有多个联系人。点击删除按钮后,相应的添加EditText和删除按钮应该消失。
这将是这样的,
--------- X
--------- X
------- - X
--------- X
———————
|contact|
———————
如果我使用任何适配器,H添加空的EditText和删除按钮(X)。如果没有其他更好的选择? 最后,我需要获得在EditText中输入的所有值。
注意:单击“联系人”时,应该添加EditText和删除按钮(X)。
请给我一个清晰简单的方法来做到这一点。
谢谢!
您可以将LinearLayout
与orientation
设置为垂直。然后您可以使用您的EditText
和Button
创建另一个布局,并将它们动态添加到onClick
联系人按钮方法中的LinearLayout
。
不要忘了产生不同的ID为你新添加的意见,能够找到他们以后
X按钮然后将刚刚从布局中删除其父
谢谢!这种做法的工作原理 – ir05
..不要忘记接受答案,以便其他人也可以受益 –
创建的LinearLayout和按钮。在按钮上单击调用以下方法。此外,在您的OnCreate方法中创建一个列表以跟踪所有添加的编辑文本&以后检索文本。
private void createNewEditText() {
EditText editText = new EditText(this);
editText.setMaxLines(1);
editText.setSingleLine(true);
editText.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, VectorDrawableCompat.create(resources, R.drawable.ic_clear_black_24dp, null), null);
editText.setOnTouchListener(new View.OnTouchListener {
@Override
Boolean onTouch(View view,MotionEvent event) {
val DRAWABLE_BOTTOM = 3
val DRAWABLE_RIGHT = 2
val DRAWABLE_LEFT = 0
val DRAWABLE_TOP = 1
if (event.getRawX() >= (editText.right - editText.getCompoundDrawables()[DRAWABLE_RIGHT].bounds.width())) {
editTextList.remove(editText);
parentLinearLayout.removeView(editText);
return true;
}
return false;
}
})
editText.requestFocus();
editText.setText(voicetext);
editTextList.add(editText);
parentLinearLayout.addView(editText);
}
看一看[这](https://stackoverflow.com/questions/24320577/edittext-and-buttons-in-listview-android) –
请参阅下面的问题。 https://stackoverflow.com/questions/12663443/add-delete-option-with-dynamically-generated-edittext?noredirect=1&lq=1 https://stackoverflow.com/questions/43666301/android-dynamically-add-edittext -in-linearlayout –
[添加删除选项与动态生成的editText](https://stackoverflow.com/questions/12663443/add-delete-option-with-dynamically-generated-edittext) –