我想为EditText框设置最小和最大输入值。我正在为EditText创建一个简单的验证;它需要A-Z和0-9值,最少5个,最多8个字符。如何在Android中为EditText设置最小字符数和最大字符数限制?
我设置的最大和其他验证如下:
android:id="@+id/edittextKode_Listing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_alignTop="@+id/textKode_listing"
android:layout_toRightOf="@+id/textKode_listing"
android:maxLength="8"
android:inputType="textCapCharacters"
android:digits="0,1,2,3,4,5,6,7,8,9,ABCDEFGHIJKLMNOPQRSTVUWXYZ"
/>
,但不能设置最小值要求。我的EditText框处于警告对话框中。我试了下面的代码来解决这个问题:
private void openInboxDialog() {
LayoutInflater inflater = this.getLayoutInflater();
// declare dialog view
final View dialogView = inflater.inflate(R.layout.kirim_layout, null);
final EditText edittextKode = (EditText) dialogView.findViewById(R.id.edittextKode_Listing);
final EditText edittextalamat = (EditText) dialogView.findViewById(R.id.edittextAlamat);
edittextKode.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(edittextKode.getText().toString().length() > 0){
if(edittextKode.getText().toString().length() < 5)
{
edittextKode.setError("Error");
Toast.makeText(GPSActivity.this, "Kode listing value not be less than 5", Toast.LENGTH_SHORT).show();
edittextKode.requestFocus();
}
}
}
});
final AlertDialog.Builder builder = new AlertDialog.Builder(GPSActivity.this);
builder.setTitle("Kirim").setView(dialogView)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
gpsCoordinates = (TextView) findViewById(R.id.text_GPS_Coordinates);
kode = edittextKode.getText().toString();
alamat = edittextalamat.getText().toString();
catatan = edittextcatatan.getText().toString();
pengirim = edittextPengirim.getText().toString();
if (kode.length() > 0 && alamat.length() > 0
&& catatan.length() > 0 && pengirim.length() > 0) {
message = "Kode listing : " + kode + "\nAlamat : "
+ alamat + " \nCatatan : " + catatan + " \n Pengirim : " + pengirim
+ "\nKoordinat GPS : "
+ gpsCoordinates.getText().toString();
sendByGmail();
} else {
Toast.makeText(
getApplicationContext(),
"Please fill all three fields to send mail",
Toast.LENGTH_LONG).show();
}
}
});
builder.create();
builder.show();
}`
在这个警报对话框中,我有两个EditText框。我想在第一个EditText上应用我的验证。我打电话给setOnFocusChangeListener检查焦点更改的最小长度。如果长度小于5则请求焦点,但仍然键入第二个EditText框。
任何人都可以请帮助我。