正常情况下,直接用EditText,无论直接在前台和后台设置setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);是没问题的。
如果EditText在自定义控件中包含,并且同时设置其他属性的时候,需要注意顺序问题,setInputType需要放在setSingleLine后面,不知道算不算android的一个Bug。
下面是本人写的一个控件,默认是文本,点击变输入框。
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sharera.capitalcircle.R;
/**
* 可编辑文本框
*
* @author Administrator
*
*/
public class EditWithText extends LinearLayout {
private TextView tv;
private EditText et;
private LinearLayout ll;
private int inPutType;
private Activity activity = new Activity();
private Calendar c = Calendar.getInstance();
// 标记是否能被编辑
private boolean isEditable = true;
public void SetActivity(Activity activity) {
this.activity = activity;
}
public EditWithText(Context context) {
this(context, null);
}
public EditWithText(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.edit_with_text, this,
true);
tv = (TextView) findViewById(R.id.edit_with_text_t);
et = (EditText) findViewById(R.id.edit_with_text_e);
ll = (LinearLayout) findViewById(R.id.edit_with_text_layout);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.EditWithText);
// 设置编辑框字
CharSequence text = a.getText(R.styleable.EditWithText_android_text);
if (text != null)
tv.setText(text);
// 设置提示字
CharSequence hint = a.getText(R.styleable.EditWithText_android_hint);
if (hint != null)
tv.setHint(hint);
// 设置内容位置
int gravity = a.getInt(R.styleable.EditWithText_android_gravity, -100);
if (gravity != -100) {
ll.setGravity(gravity);
tv.setGravity(gravity);
}
// 设置最长度
int maxLength = a
.getInt(R.styleable.EditWithText_android_maxLength, 25);
InputFilter[] filters = { new LengthFilter(maxLength) };
et.setFilters(filters);
// 设置字大小
String size = a.getString(R.styleable.EditWithText_android_textSize);
if (size != null && size.length() > 0) {
size = size.substring(0, size.length() - 2);
et.setTextSize(TypedValue.COMPLEX_UNIT_SP, Float.valueOf(size));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, Float.valueOf(size));
}
// 设置是否多行
Boolean isMulti = a.getBoolean(
R.styleable.EditWithText_android_singleLine, true);
et.setSingleLine(isMulti);
// 设置内容类型
inPutType = a.getInt(R.styleable.EditWithText_android_inputType, -100);
if (inPutType != -100) {
et.setInputType(inPutType);
}
if (!isMulti) {
tv.setGravity(Gravity.TOP);
et.setGravity(Gravity.TOP);
MarginLayoutParams tvMarginsParams = new MarginLayoutParams(
tv.getLayoutParams());
tvMarginsParams.setMargins(0, 15, 0, 13);
tv.setLayoutParams(new LayoutParams(tvMarginsParams));
MarginLayoutParams etMarginsParams = new MarginLayoutParams(
et.getLayoutParams());
etMarginsParams.setMargins(0, 10, 0, 13);
et.setLayoutParams(new LayoutParams(etMarginsParams));
InputFilter[] filters1 = { new LengthFilter(200) };
et.setFilters(filters1);
} else {
tv.setGravity(Gravity.CENTER_VERTICAL);
et.setGravity(Gravity.CENTER_VERTICAL);
}
a.recycle();
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!isEditable)
return;
// 日期
if (inPutType == 20) {
// TODO Auto-generated method stub
new DatePickerDialog(EditWithText.this.activity,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view,
int year, int monthOfYear,
int dayOfMonth) {
// 设置日历
EditWithText.this.tv.setText(year + "-"
+ monthOfYear + "-" + dayOfMonth);
EditWithText.this.et.setText(year + "-"
+ monthOfYear + "-" + dayOfMonth);
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
.get(Calendar.DAY_OF_MONTH)).show();
}
else if (tv.getVisibility() == View.VISIBLE) {
et.setText(tv.getText().toString());
tv.setVisibility(View.GONE);
et.setVisibility(View.VISIBLE);
et.performClick();
et.setFocusable(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
et.requestFocusFromTouch();
InputMethodManager inputManager = (InputMethodManager) et
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(et, 0);
}
}
});
}
// 设置值
public void setText(String text) {
tv.setText(text);
// 增加对EditText赋值
et.setText(text);
}
// 获取值
public String getText() {
return et.getText().toString().trim();
}
// 是否具有编辑属性
public boolean isEditable() {
return isEditable;
}
// 设置编辑属性
public void setEditable(boolean isEditable) {
this.isEditable = isEditable;
}
// 结束编辑
public void finishEdit() {
tv.setVisibility(View.VISIBLE);
et.setVisibility(View.GONE);
// tv.setGravity(Gravity.RIGHT);
setText(getText());
}
}