我正尝试以编程方式在窗体中创建电话字段。如何识别并随后获得其价值?如何以编程方式创建EditText,然后获取其值
,创造了EditText
领域,并添加到窗体中的代码是这样的:
public void addTelephone(){
//form
TableLayout table = (TableLayout)findViewById(R.id.table);
//new row
TableRow tr = new TableRow(NewRequestActivity.this);
LayoutParams ltr = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ltr.setMargins(0, 0, 0, 2500);
tr.setLayoutParams(ltr);
//new field
EditText et = new EditText(NewRequestActivity.this);
et.setHint("telephone");
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.requestFocus();
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.input_form)/getResources().getDisplayMetrics().density;
et.setTextSize(fontSize);
LayoutParams let = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
et.setLayoutParams(let);
ImageView img = new ImageView(NewRequestActivity.this);
img.setTag("img_"+counttelf);
img.setImageResource(R.drawable.more);
img.setBackgroundResource(R.drawable.imagefocused);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
addTelephone();
}
});
LayoutParams limg = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
img.setLayoutParams(limg);
tr.addView(et, 0);
tr.addView(img, 1);
table.addView(tr, ++indextelf);
}
检查:http://stackoverflow.com/questions/23577420/how-to-get-id-of-edit-text-on-click-of-dynamically-created -edit-text-in-android/23577508#23577508 – 2014-10-01 07:07:04