Domanda

Ho un testo in cui voglio inserire il valore percentuale, cioè il valore decimali, quindi voglio limitare l'utente a inserire solo un valore inferiore a 100 nel testo di modifica.

Esempio: 1) se desidera inserire un valore superiore a 100 , non dovrebbe consentirgli di inserirlo.

2) Se desidera che t0 inserisca un valore decimale inferiore a 100, ovvero 50,5444 , dovrebbe consentirgli di inserire questo valore

Ho anche trovato questo link dove posso impostare il filtro per il valore intero massimo su 100 ma non mi consente di inserire un valore decimale

link: http://tech.chitgoks.com/2011/06/27/android-set-min-max-value-an-edittext-accepts/

Qualcuno può aiutarmi.

È stato utile?

Soluzione

Ehi, puoi utilizzare TextWatcher , applicalo al tuo EditTexte implementare i tre metodi che fornisce.Quindi, quando inserisci un nuovo valore, puoi utilizzare una RegEx per verificarne la validità.

Spero che aiuti!

Altri suggerimenti

Ecco il codice di implementazione completo che ho usato

editTextSpLinePercent.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        String enteredValue = s.toString();
        if(checkNullValues(enteredValue)) {
            if(Float.parseFloat(enteredValue.trim()) >100.0f){
                AlertDialog.Builder builder = new AlertDialog.Builder(SpecialDiscountLineActivity.this);
                // builder.setCancelable(true);
                builder.setMessage("Percentage Value should be Less than 100");

                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                         editTextSpLinePercent.setText("");
                    }
                });           
                builder.show();            
            }
        }
    }
});


public static boolean checkNullValues(String valueToCheck) {
    //Log.i("Log","CheckForNullValues : "+valueToCheck);
    if(!(valueToCheck == null)) {
        String valueCheck = valueToCheck.trim();
        if(valueCheck.equals("") || valueCheck.equals("0")  ) {
            //  Log.i("Log","Returning false 0 or Blank");
            return false;
        }
        return true;
    }
    //Log.i("Log","Returning false null");
    return false;
}

Puoi effettivamente modificare l'utile sniplet di codice fornito nel collegamento menzionato nella domanda come segue.Inoltre, facoltativamente, puoi includere il numero di cifre decimali massime che desideri che la percentuale abbia e utilizzare il pattern matcher per restituire "" se l'input non corrisponde al modello desiderato.

private class PercentageInputFilter implements InputFilter {
    private float min;
    private float max;

    PercentageInputFilter(float min, float max) {
        this.min = min;
        this.max = max;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            // Get input
            String stringToMatch = dest.toString() + source.toString();
            float input = Float.parseFloat(stringToMatch);

            // Check if the input is in range.
            if (isInRange(min, max, input)) {
                // return null to accept the original replacement in case the format matches and text is in range.
                return null;
            }
        } catch (NumberFormatException nfe) {
        }
        return "";
    }

    private boolean isInRange(float min, float max, float input) {
        return input >= min && input <= max;
    }
}

Puoi usarlo come:

new PercentageInputFilter((float) 0.00, (float) 100.00);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top