操作在输入对话框中完成

时间:2012-11-19 22:19:18

标签: android

我有一个标准的输入弹出对话框

    AlertDialog.Builder builder = new AlertDialog.Builder(activity);

    builder.setTitle(title)
        .setMessage(message)
        .setPositiveButton(buttonPositif, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Send the positive button event back to the host activity
                   mListener.onDialogPositiveClick(EntryDialogFacade.this);
               }
           })
        .setNegativeButton(buttonNegatif, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Send the negative button event back to the host activity
                   mListener.onDialogNegativeClick(EntryDialogFacade.this);
               }
           });        

    editText = new EditText(activity);
    editText.setLines(1);
    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
    builder.setView(editText);

    Dialog dialog = builder.create();        
    dialog.show();

我正在尝试在输入字段键盘上显示“完成”按钮(请参阅代码段),但是,这不是这样的。

有没有人经历过同样的事情?

干杯。

1 个答案:

答案 0 :(得分:4)

您需要使用setSingleLine(true)而不是setLines(1),然后需要EditorActionListener来抓住Done密钥。

editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setOnEditorActionListener(new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        Toast.makeText(MainActivity.this, "Got IME Done", Toast.LENGTH_SHORT).show();
        return true;
    }
});