我想创建一个onclick事件,创建一个EditText. 我尝试了以下代码,但它会擦除所有内容并创建一个带有EditText.
public class MainActivity extends Activity {
ArrayList<Contact> contact;
Contact currentcontact;
EditText nameArea,emailArea,phoneArea;
int emails=1;
int phones=1;
@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){
}
}
protected void createemaileditview(){
LinearLayout outerLayout=new LinearLayout(this);
outerLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
outerLayout.setOrientation(LinearLayout.VERTICAL);
EditText email=new EditText(this);
email.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,1f));
outerLayout.addView(email);
setContentView(outerLayout);
emails++;
}
protected void createphoneeditview(){
phones++;
}
}
人到中年有点甜
MMMHUHU
相关分类