我正在使用Java开发Android 2.2。 我在PopupWindow上放置了一个EditText,但它无法正常工作。 它的行为就像是一个禁用的EditText,单击EditText不会显示软键盘。 如何在PopupWindow中添加EditText?
我正在使用Java开发Android 2.2。 我在PopupWindow上放置了一个EditText,但它无法正常工作。 它的行为就像是一个禁用的EditText,单击EditText不会显示软键盘。 如何在PopupWindow中添加EditText?
尝试一下:
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();
我是这样解决问题的:我将popupWindow.setFocusable(true);
放入代码中,现在它可以工作了。看来弹出窗口中的编辑框没有焦点是因为弹出窗口本身没有焦点。
popWindow.setFocusable(true);
popWindow.update();
它会起作用的。
从任何监听器中调用此代码
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();
}
EditText是否一定将android:editable属性设置为true?如果设置为false,则会禁用它,正如您所描述的那样。