Question

I have a Dialog Fragment that allows a user to specify some text, and a position for the text.

dialog fragment

I want to restrict the user from entering text greater than some upper limit of characters. Can I restrict the max character input? Else, where in this code can I include a check to make sure the text is less than LIMIT? Here is the code:

public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final CharSequence[] items = {"Above", "Below"};
    capPos = Position.ABOVE;

    final EditText input = new EditText(getActivity());
    input.setInputType(EditorInfo.TYPE_CLASS_TEXT);
    input.setHint("Add caption here");
    builder.setView(input);

    builder.setTitle(R.string.dialog_caption)
           .setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                text = input.getText().toString();
                mListener.onDialogPositiveClick(CaptionFragment.this);
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   mListener.onDialogNegativeClick(CaptionFragment.this);
               }
           })
            .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id){
                    if ("Above".compareTo(items[id].toString()) == 0){
                        capPos = Caption.Position.ABOVE ;
                        Log.i("CaptionFragment", "Above");
                    }else if ("Below".compareTo(items[id].toString()) == 0){
                        capPos = Caption.Position.BELOW;
                        Log.i("CaptionFragment", "Below");
                    }
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
} 
Était-ce utile?

La solution

So it is an EditText. You can use the attiribute android:maxLength to limit the number of characters as shown here https://stackoverflow.com/a/3285421/655987

Another option is to let the user to enter whatever he/she wants and then do the check background.

You can do that inside your OnClickListener. If you want to check the value only when user clicks the positive button -which usually is the case-, then you should do it as follows:

...
.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
            text = input.getText().toString();
            if(text.equals("ads"))  //For example
                ...
           }
       })
...

Also, you can initially set the onClickListener of the positive button to null and then override it as shown here https://stackoverflow.com/a/7636468/655987

It is up to you.

Autres conseils

try this,

final EditText input = new EditText(getActivity());
    input.setInputType(EditorInfo.TYPE_CLASS_TEXT);
input.setFilters(new InputFilter[] {
            new InputFilter.LengthFilter(LIMIT) });
    input.setHint("Add caption here");
    builder.setView(input);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top