弹出窗口中的EditText

16

我正在使用Java开发Android 2.2。 我在PopupWindow上放置了一个EditText,但它无法正常工作。 它的行为就像是一个禁用的EditText,单击EditText不会显示软键盘。 如何在PopupWindow中添加EditText?

5个回答

47

尝试一下:

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Title");
alert.setMessage("Message");

// Set an EditText view to get user input 
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

  // Do something with value!
  }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    // Canceled.
  }
});

alert.show();

1
弹出窗口差点让我浪费了一整天,但最终在朋友的帮助下只花了几分钟就解决了我的问题。谢谢你啊伙计! - Khurram Iqbal

18

我是这样解决问题的:我将popupWindow.setFocusable(true);放入代码中,现在它可以工作了。看来弹出窗口中的编辑框没有焦点是因为弹出窗口本身没有焦点。


我按照你的建议尝试了,但这并没有帮助。 - Deepak
1
但是仍然存在一个问题,即当您长按EditText时,标记不会出现,也不会给出剪贴板粘贴。 - Khurram Iqbal

0
popWindow.setFocusable(true);
popWindow.update();

它会起作用的。


0

从任何监听器中调用此代码

private void popUpEditText() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Comments");

        final EditText input = new EditText(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        input.setLayoutParams(lp);
        builder.setView(input);

        // Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

             // do something here on OK 

            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();

    }

0

EditText是否一定将android:editable属性设置为true?如果设置为false,则会禁用它,正如您所描述的那样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接