Java 弹出窗口上的 EditText

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4134117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:58:16  来源:igfitidea点击:

EditText On A Popup Window

javaandroidandroid-edittextpopupwindow

提问by Laura

I am developing on Android 2.2 using Java. I put an editText on a PopupWindow and it's not working. 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?

我正在使用 Java 在 Android 2.2 上进行开发。我在 PopupWindow 上放置了一个 editText 但它不起作用。它就像一个禁用的编辑文本,点击编辑文本不会显示软键盘。如何在弹出窗口上添加编辑文本?

采纳答案by Laura

I have resolved the problem like this: I put the 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.

我已经解决了这样的问题:我把popupWindow.setFocusable(true);它现在工作了。似乎弹出窗口上的编辑文本没有焦点,因为弹出窗口没有焦点。

回答by Sinjo

Does the EditText definitely have the android:editable property set to true? If it's false it will be disabled as you describe.

EditText 是否肯定将 android:editable 属性设置为 true?如果它是假的,它将按照您的描述被禁用。

回答by Siten

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

回答by kamleshwer purohit

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

It will work.

它会起作用。

回答by Mohan

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

    }