! , .
, , , edittext. , 0-5 , 0-9 . , , 0-59 1-60, , .
onCreate :
final InputFilter filter = new InputFilter()
{
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dent)
{
for (int i = start; i < end; i++)
{
if ((source.charAt(start) == "6".charAt(0)) || (source.charAt(start) == "7".charAt(0)) || (source.charAt(start) == "8".charAt(0))
|| (source.charAt(start) == "9".charAt(0)) || (!Character.isDefined(source.charAt(i))) )
{
return "";
}
}
return null;
}
};
private class FilterCheckerTask extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params)
{
while(true)
{
if (<EditText>.getText().toString().isEmpty())
{
Log.e("empty","empty");
<EditText>.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(2)});
}
else if (<EditText>.getText().toString().charAt(0) >= "6".charAt(0))
{
Log.e("front num bad","greater than 5");
<EditText>.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(2)});
}
else
{
Log.e("unfiltered", "unfiltered");
<EditText>.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
}
if (kicker)
{
return null;
}
}
}
}
onCreate:
Time_sec.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
new FilterCheckerTask().execute();
if(!hasFocus)
{kicker = !hasFocus;}
}
});
As I said in the first part, this will make it so that the user can enter only numbers from 00 to 59 in the edittext field. I know that this may look a bit messy, and it can probably be cleaned up a bit in some if operations, but as far as I can tell, it works absolutely fine and doesn't seem to slow down the system. I hope this helps you, and if not, future googlers.
source
share