InputFilter

 

package com.example.testdialog;

import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private static final int MAX_INPUT_LENGTH = 6;    
    private static final String REGEX = "[^*\"?|<>/]{1,}";

    protected static final String TAG = "CM-MainActivity";
    private Button mButton;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(this);
        mTextView = (TextView) findViewById(R.id.textView);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                showAlertDialog();
                break;
            default:
                break;
        }
    }

    private void showAlertDialog() {
        final EditText editText = new EditText(this);
        editText.setSingleLine();
        
        final AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(
                "change text").create(); 
        
        editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
                MAX_INPUT_LENGTH) {
            @Override
            public CharSequence filter(CharSequence source, int start, int end,
                    Spanned dest, int dstart, int dend) {
                // no matter how long the string length is, this filter will be
                // triggered.

                // the text above the input method
                // source: the text above the input method, it is going to be
                // input.
                // start: the beginning index of source
                // end: the end index of source

                // The text which is going to be input in EditText, excluding last char
                // dest: the text in EditText which is going to be input.
                //
                // dstart: the start index of the text in EditText which is going to be input.
                // dend: the end index of the text in EditText which is going to be input,
                //       excluding last char
                Log.d(TAG, "filter() source:" + source + " start:" + start
                        + " end:" + end);

                Log.d(TAG, "filter() dest:" + dest.toString() + " dstart:"
                        + dstart + " dend:" + dend);
                if (dest.length() == 3) {
                    Toast.makeText(MainActivity.this, "too long",
                            Toast.LENGTH_SHORT).show();
                }

                return super.filter(source, start, end, dest, dstart, dend);
            }
        } });
        
        editText.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable arg0) {               
                
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > before) {
                    CharSequence text = s.subSequence(start + before, start + count);
                    if (containInvalidCharacter(text)) {
                        Toast.makeText(MainActivity.this, "invalid string", Toast.LENGTH_SHORT).show();
                    }
                }
                
                alertDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(!containInvalidCharacter(s));
                
            }});

        alertDialog.setButton(Dialog.BUTTON_POSITIVE,
                getString(android.R.string.ok),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String newName = editText.getText().toString().trim();
                        Log.d(TAG, "new name:" + newName);
                        mTextView.setText(newName);
                    }
                });

        alertDialog.setButton(Dialog.BUTTON_NEGATIVE,
                getString(android.R.string.cancel),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // do nothing
                    }
                });

        alertDialog.setView(editText);        
        alertDialog.show();
        alertDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
    }

    private boolean containInvalidCharacter(CharSequence s) {
        return !Pattern.compile(REGEX).matcher(s).matches();
    }
}

 

posted @ 2015-04-03 20:22  牧 天  阅读(544)  评论(0)    收藏  举报