Android EditText:自定义AlertDialog中的软键盘
我实现了一个弹出对话框,里面有EditText元素。我无法在屏幕上显示软键盘,因此无法填充EditText元素。这个问题是众所周知的,但我仍然不能让它工作。我尝试了不同的方法来解决这个问题-请参阅onCreate方法。谢谢Android EditText:自定义AlertDialog中的软键盘,android,Android,我实现了一个弹出对话框,里面有EditText元素。我无法在屏幕上显示软键盘,因此无法填充EditText元素。这个问题是众所周知的,但我仍然不能让它工作。我尝试了不同的方法来解决这个问题-请参阅onCreate方法。谢谢 public class MyPopup extends AbstractPlainPopup { protected Context _context; public CreatePlaylistPopup(Context context) {
public class MyPopup extends AbstractPlainPopup {
protected Context _context;
public CreatePlaylistPopup(Context context) {
super(context);
_context = context;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = getLayoutInflater();
View container = inflater.inflate(R.layout.popup_new_playlist, null);
final EditText titleInput = (EditText) container.findViewById(R.id.my_text_view);
titleInput.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager mgr = (InputMethodManager) _context.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(titleInput, InputMethodManager.SHOW_IMPLICIT);
//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//MyPopup.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
}
});
container.findViewById(R.id.cancelButton).setOnClickListener(
new onCancelClick());
container.findViewById(R.id.createButton).setOnClickListener(
new onCreateClick());
setContentView(container);
}
abstract public class AbstractPlainPopup extends AlertDialog implements Observable {
public final static int CANCEL = 0;
public final static int OK = 1;
protected int _state;
protected ArrayList<Observer> observers = new ArrayList<Observer>();
public AbstractPlainPopup(Context context){
super(context);
}
public AbstractPlainPopup(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
Dialog dialog = new Dialog(this, R.style.Theme_Dialog_Transparent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.enter_details);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
公共类MyPopup扩展了MyPopup{
保护上下文(protectedcontext)上下文;;
公共CreatePlayPlayPopup(上下文){
超级(上下文);
_上下文=上下文;
}
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LayoutInflater充气机=getLayoutInflater();
查看容器=充气机。充气(R.layout.popup\u new\u playlist,空);
最终EditText标题输入=(EditText)container.findViewById(R.id.my_text_视图);
titleInput.setOnFocusChangeListener(新视图.OnFocusChangeListener(){
@凌驾
public void onFocusChange(视图v,布尔hasFocus){
如果(hasFocus){
InputMethodManager mgr=(InputMethodManager)\u context.getSystemService(context.INPUT\u METHOD\u SERVICE);
经理showSoftInput(titleInput、InputMethodManager.SHOW_隐式);
//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入状态\u可见);
//MyPopup.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入\状态\可见);
}
}
});
container.findviewbyd(R.id.cancelButton).setOnClickListener(
新的onCancelClick());
container.findviewbyd(R.id.createButton).setOnClickListener(
新建onCreateClick());
setContentView(容器);
}
抽象公共类AbstractPlainPopup扩展AlertDialog实现可观察{
公共最终静态int CANCEL=0;
公共最终静态int OK=1;
受保护的国际国家;
受保护的ArrayList观察员=新建ArrayList();
公共摘要(上下文){
超级(上下文);
}
public AbstractPlainPopup(上下文上下文、布尔可取消、OnCancelListener cancelListener){
super(上下文、可取消、取消侦听器);
}
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u STATE\u ALWAYS\u VISIBLE);
Dialog dialog = new Dialog(this, R.style.Theme_Dialog_Transparent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.enter_details);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
在onCreate()中使用此选项而且不在焦点更改侦听器内。仍然不起作用。如果您对您的建议有信心,请-您可能希望发布您的示例。我非常感谢。是的,但它不是AlertDialog对象。无论如何,我也做了同样的事,只是为边框添加了透明度
Dialog dialog = new Dialog(this, R.style.Theme_Dialog_Transparent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.enter_details);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);