EditText:Custom AlertDialog中的SoftKeyboard

时间:2012-06-09 10:42:41

标签: android

我实现了一个内置EditText元素的弹出对话框。我无法在屏幕上显示Softkeyboard,因为它无法填充EditText元素。问题是众所周知的,但我仍然无法使其发挥作用。我已经尝试了不同的选项来解决这个问题 - 请参阅onCreate方法。感谢。

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

在onCreate()中使用此功能,而不是在焦点更改侦听器

中使用