In my EditText
字段,我想给一些最小文本作为强制,最大文本作为限制,有什么方法可以实现这一点吗?
如果要输入文本,则数字计数必须减少。我该怎么做呢?
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="24dp"
android:maxLength="175"
android:ems="10" />
这是我添加的activity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
System.out.println(PRAYER_CATEGORY.length);
tvPrayer = (TextView) findViewById(R.id.mystate);
spinnerPrayers = (Spinner) findViewById(R.id.spinnerstate);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, PRAYER_CATEGORY);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerPrayers.setAdapter(adapter_state);
value=(EditText)findViewById(R.id.editText1);
value
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (value.getText().toString().trim()
.length() < 3) {
value.setError("Failed");
} else {
value.setError(null);
}
}
else {
if (value.getText().toString().trim()
.length() < 3) {
value.setError("Failed");
} else {
value.setError(null);
}
}
}
});
btnSpeakprayer = (ImageButton) findViewById(R.id.btnSpeakprayer);
btn=(Button)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
你可以试试这个代码
首先,在 xml 文件中设置 maxlength,如下所示
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:inputType="textPassword"
android:lines="1"
android:maxLength="15"
android:maxLines="1"
android:singleLine="true" />
然后在你的代码中你可以这样写
et_billamt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (et_billamt.getText().toString().trim().length() < 5) {
et_billamt.setError("Failed");
} else {
// your code here
et_billamt.setError(null);
}
} else {
if (et_billamt.getText().toString().trim().length() < 5) {
et_billamt.setError("Failed");
} else {
// your code here
et_billamt.setError(null);
}
}
}
});
我是在没有焦点之后设计的,所以这里你可以写最小长度条件和最大长度条件
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)