如何在 EditText 中设置最小文本(必填)和最大文本数
2022-09-04 01:50:28
在我的字段中,我想给出一些最小文本作为强制性,最大文本作为限制,有没有办法实现这一目标?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" />
这是我的添加活动.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);