EditText的代码设置

最近工作中用到了动态设置EditText控件的内容:

动态设置EditText的输入格式需要在最后设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
editText = new EditText(context);
editText.setBackground(null);
editText.addTextChangedListener(this);
editText.setHint(mHint);
editText.setHintTextColor(getResources().getColor(R.color.cellview_edite_texthintcolor));
editText.setTextColor(getResources().getColor(R.color.cellview_edite_textcolor));
editText.setTextSize(DisplayUtil.dip2px(context, DEFALT_TEXT_SIZE));
editText.setGravity(Gravity.RIGHT);

if (length > 0) {
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
}
//

editText.setSingleLine();
editText.measure(0, 0);
w = DisplayUtil.getScreenWith(context) - w - 2 * padLeft;
h = editText.getMeasuredHeight();
MarginLayoutParams editTextmp = new MarginLayoutParams(w
, h); //item的宽高
editTextmp.setMargins(0, mt, padLeft, 0);//分别是margin_top那四个属性
RelativeLayout.LayoutParams editeparams = new RelativeLayout.LayoutParams(editTextmp);
editeparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// editeparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
editText.setLayoutParams(editeparams);
if (isPassword) {
// editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
addView(editText);

分享到 评论