默认焦点和键盘的EditText在Android的AlertDialog [英] Default Focus and Keyboard to EditText in Android AlertDialog

查看:354
本文介绍了默认焦点和键盘的EditText在Android的AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android上AlertDialog.Builder快速提示文本的用户。该对话框显示了伟大工程,但用户必须点击的EditText字段来加载软键盘。有什么办法打开键盘,并给予重点的,只要打开我的对话框?这是我的code:

 最终地图<字符串,对象> rowData = itemList.get(mPosition);
                    最后的EditText输入=新的EditText(searchList.getContext());
                input.requestFocus();


                input.setSingleLine();
                最后AlertDialog对话框=新AlertDialog.Builder(searchList.getContext())
                .setTitle(StringUtils.getSafeString(rowData.get(标签)))
                .setView(输入)
                .setPositiveButton(OK,新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释whichButton){
                        rowData.put(价值,StringUtils.getSafeString(input.getText()));
                        sea​​rchList.invalidateViews();

                    }
                })。setNegativeButton(取消,新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释whichButton){
                        // 没做什么。
                    }
                })。创建();
                dialog.show();
 

解决方案

使用下面的code。它为我工作。

  editText.setOnFocusChangeListener(新OnFocusChangeListener(){
        @覆盖
        公共无效onFocusChange(视图V,布尔hasFocus){
            editText.post(新的Runnable(){
                @覆盖
                公共无效的run(){
                    InputMethodManager inputMethodManager =(InputMethodManager)YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(EDITTEXT,InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    editText.requestFocus();
 

I am using the AlertDialog.Builder in Android to quickly prompt the user for text. The dialog shows up and works great, but the user must click on the EditText field to load the soft keyboard. Is there any way to open the keyboard and give focus to the whenever my dialog is opened? Here is my code:

final Map<String,Object> rowData = itemList.get(mPosition);
                    final EditText input = new EditText(searchList.getContext());
                input.requestFocus();


                input.setSingleLine();
                final AlertDialog dialog = new AlertDialog.Builder(searchList.getContext())
                .setTitle(StringUtils.getSafeString(rowData.get("label")))
                .setView(input)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) { 
                        rowData.put("value", StringUtils.getSafeString(input.getText()));
                        searchList.invalidateViews();

                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Do nothing.
                    }
                }).create();
                dialog.show();

解决方案

Use the following code. It worked for me.

    editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            editText.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager inputMethodManager= (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    editText.requestFocus();

这篇关于默认焦点和键盘的EditText在Android的AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆