简体   繁体   English

弹出窗口上的EditText

[英]EditText On A Popup Window

I am developing on Android 2.2 using Java. 我正在使用Java在Android 2.2上进行开发。 I put an editText on a PopupWindow and it's not working. 我在PopupWindow上放了一个editText,它不起作用。 It acts like a disabled edit text, clicking on the edit text won't show the soft keyboard. 它就像一个禁用的编辑文本,单击编辑文本将不会显示软键盘。 How can I add an edit text on a popupWindow? 如何在popupWindow上添加编辑文本?

Just try: 试一试:

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();

I have resolved the problem like this: I put the popupWindow.setFocusable(true); 我已经解决了这个问题:我把popupWindow.setFocusable(true); and now it's working. 现在它正在发挥作用。 It seems that the edit text which was on a pop window didn't have focus because the popup window didn't have focus. 看起来弹出窗口上的编辑文本没有焦点,因为弹出窗口没有焦点。

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

It will work. 它会工作。

Does the EditText definitely have the android:editable property set to true? EditText肯定将android:editable属性设置为true吗? If it's false it will be disabled as you describe. 如果它是假的,它将被禁用,如您所述。

call this code from any listener 从任何监听器调用此代码

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();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM