Android 添加/删除编辑文本并单击删除(X)
我是android开发新手 我想在单击按钮(称为“联系人”)时添加EditText和remove按钮(X)。这样会有多个联系人。单击“删除”按钮时,相应添加的带有“删除”按钮的EditText将消失 会是这样的, ---------XAndroid 添加/删除编辑文本并单击删除(X),android,android-edittext,Android,Android Edittext,我是android开发新手 我想在单击按钮(称为“联系人”)时添加EditText和remove按钮(X)。这样会有多个联系人。单击“删除”按钮时,相应添加的带有“删除”按钮的EditText将消失 会是这样的, ---------X ——————— |contact| ——————— ---------X ——————— |contact| ——————— ---------X ——————— |contact| ——————— ---------X —————
———————
|contact|
———————
---------X
———————
|contact|
———————
---------X
———————
|contact|
———————
---------X
———————
|contact|
———————
如果我使用任何适配器,如何添加空的EditText和移除按钮(X)。如果没有其他更好的选择?
最后,我需要获取EditText中输入的所有值
注:点击“联系人”时,应添加编辑文本和删除按钮(X)
请给我一个明确和简单的方法来做到这一点
谢谢 您可以使用
线性布局
,将方向设置为垂直。然后,您可以使用EditText
和按钮创建另一个布局,并将它们动态添加到联系人按钮的onClick
方法中的LinearLayout
不要忘记为新添加的视图生成不同的ID,以便以后能够找到它们
然后,X按钮将从布局中删除其父按钮创建一个线性布局
和按钮。在按钮上单击调用以下方法。另外,在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);
}
看看下面的问题。可能是@Pier Giorgio Misley的副本,我尝试了链接中给出的解决方案,getView()方法未被调用。@SARATHV我检查了链接,我正在寻找不同的方法。谢谢好。。别忘了接受答案,这样别人也能从中受益