当超过 属性 Maxlenght EditText 时显示 AlertDialog

Show AlertDialog when the property Maxlenght a EditText are exceeded

我现在要做的,就是弹出一个对话框,当你超过EditText的字符插入量时,在XML文件中将maxlenght设置为“15”,之后用户可以继续按键盘但当然无济于事,然后我会弹出对话框(AlertDialog),指示用户已超出输入的字符限制。

我向您展示了创建的方法、发送消息的操作、控件的 AlertDialog 和限制字符的 AlertDialog

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String messaggio = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, messaggio);
    if (messaggio.equals("")) {
        finestraInfo();
    }else {
        startActivity(intent); 
        editText.setText("");
    }
}

//AlertDialog message empty
public void finestraInfo() {
    AlertDialog.Builder info = new AlertDialog.Builder(this);
    info.setTitle("Info");
    info.setMessage("Empty message, enter message!");
    info.setPositiveButton("Ok", null);
    info.create().show();
}

//AlerDialog limited
public void infoLimiteParole(){
    AlertDialog.Builder info = new AlertDialog.Builder(this);
    info.setTitle("Info");
    info.setMessage("Character limit exceeded.!");
    info.setPositiveButton("Ok", null);
    info.create().show();
}

如何使 EditText (id = edit_message) 与 AlertDialog 交互?我可以在什么基础上建立验证检查?

您应该通过 EditText 的 addTextChangedListener() 方法使用 TextWatcher

EditText myText = (EditText) findViewById(R.id.text);
myText.addTextChangedListener(new TextWatcher()
{
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
        if (s.length() > n)
        {
           // Show Dialog that limit is exceeded
        }
    }

    @Override
    public void afterTextChanged(Editable s)
    {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
    }
});

一次尝试如下

editText.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
            if(s.toString().length()>15)
                infoLimiteParole();
        }
    });

希望对您有所帮助。