在功能delemaileditview()
和delphoneeditview()
中应该删除现有的Editview
,但是存在未知的问题会阻止应用程序
public class MainActivity extends Activity {
ArrayList<Contact> contact;
Contact currentcontact;
EditText nameArea,emailArea,phoneArea;
int emails=0;
int phones=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameArea=findViewById(R.id.name_area);
emailArea=findViewById(R.id.e1);
phoneArea=findViewById(R.id.p1);
contact=new ArrayList<>();
}
public void buttonclick(View v){
if(v.getId()==R.id.addemail){
createemaileditview();
}
if(v.getId()==R.id.addphone){
createphoneeditview();
}
if(v.getId()==R.id.save){
}
if(v.getId()==R.id.cancel){
}
if(v.getId()==R.id.delemail){
delemaileditview();
}
if(v.getId()==R.id.delphone){
delphoneeditview();
}
}
protected void delemaileditview(){
EditText v = findViewById((R.id.e1+emails));
v.setVisibility(View.GONE);
emails--;
}
protected void delphoneeditview(){
EditText v = findViewById((R.id.p1+phones));
((ViewGroup)v.getParent()).removeView(v);
phones--;
}
protected void createemaileditview(){
Context context=getApplicationContext();
EditText designation1 = new EditText(context);
designation1.setSingleLine(true);
designation1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
designation1.setPadding(14,30,14,30);
designation1.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.editview));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.e1+emails);
RelativeLayout RL=findViewById(R.id.parentLayout);
RL.addView(designation1, params);
emails++;
designation1.setId(R.id.e1+emails);
designation1.setHint("email "+(emails+1));
}
protected void createphoneeditview(){
Context context=getApplicationContext();
EditText designation1 = new EditText(context);
designation1.setPadding(14,30,14,30);
designation1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_PHONE);
designation1.setSingleLine(true);
designation1.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.editview));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.p1+phones);
RelativeLayout RL=findViewById(R.id.parentLayout1);
RL.addView(designation1, params);
phones++;
designation1.setId(R.id.p1+phones);
designation1.setHint("phone no "+(phones+1));
}
}
答案 0 :(得分:0)
您可以使用HashMap
存储要删除的视图。
例如,电子邮件视图:
HashMap<Integer, View> emailMap = new HashMap<>();
int emailIndex = 0;
protected void delEmailEditView(){
if(emailMap.size() <= 0) return;
View v = emailMap.remove(emailIndex);
if(v == null) return;
((ViewGroup)v.getParent()).removeView(v);
emailIndex--;
// Get rid of any resources allocated to the view
v = null;
// If you just want to make it GONE
//View view = emailMap.get(emailIndex);
//view.setVisibility(View.GONE);
}
protected void createEmailEditView(){
Context context=getApplicationContext();
EditText designation1 = new EditText(context);
designation1.setSingleLine(true);
designation1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
designation1.setPadding(14,30,14,30);
designation1.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.editview));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.e1+emails);
RelativeLayout RL=findViewById(R.id.parentLayout);
RL.addView(designation1, params);
emailMap.put(emailIndex, designation1);
emailIndex++;
designation1.setHint("email " + emailIndex);
}
您可以对电话EditText
视图进行模拟。
请注意,我无能为力,只能更改方法名称以更紧密地反映Java命名约定。我建议您为所有Android项目采用Java命名约定。
免责声明:
我没有测试此代码,所以如果您有任何问题,请告诉我。