Displaying soft keyboard whenever AlertDialog.Builder object is opened

后端 未结 14 1766
半阙折子戏
半阙折子戏 2020-12-08 19:11

My code for opening an input dialog reads as follows:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);  
alert.setTitle(\"Dialog Title\");  
         


        
相关标签:
14条回答
  • 2020-12-08 20:03

    I found this worked in an Alert Dialog called from different places

            case R.id.birthyear:
            case R.id.ymax:
                input.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
    

    Mind you I tried lots of other things too. Seems delicate.

    0 讨论(0)
  • 2020-12-08 20:04

    In the following code snippet I show how to host an arbitrary LinearLayout in a DialogFragment. One using AlertDialog (where the soft keyboard doesn't work) and another way without using AlertDialog (where the soft keyboard does work!):

    public static LinearLayout createLinearLayout(Context context)
    {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        String html;
        html = "<form action=search method=get >\n";
        html += "Google Search: <input name=q value=\"Johnny Depp\" /><br/>\n";
        html += "<input type=submit name=output value=search /><br/>\n";
        html += "</form>\n";
        WebView webView = new WebView(context);
        webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        webView.loadDataWithBaseURL("http://www.google.com", html, "text/html", "UTF-8", null);
        linearLayout.addView(webView);
        return linearLayout;
    }
    
    public void showWithAlertDialog()
    {
        DialogFragment dialogFragment = new DialogFragment()
        {
            public Dialog onCreateDialog(Bundle savedInstanceState)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder
                    .setTitle("With AlertDialog")
                    .setView(createLinearLayout(getActivity()));
                return builder.create();
            }
        };
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        dialogFragment.show(fragmentTransaction, "dialog");
    }
    
    public void showWithoutAlertDialog()
    {
        DialogFragment dialogFragment = new DialogFragment()
        {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                getDialog().setTitle("Without AlertDialog");
                getDialog().setCanceledOnTouchOutside(false);
                return createLinearLayout(getActivity());
            }
        };
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        dialogFragment.show(fragmentTransaction, "dialog");
    }
    
    0 讨论(0)
  • 2020-12-08 20:06

    With the encouragement of Mur Votema (see his answer above) I have answered my question by building a custom dialog based on the Dialog class. Unlike an alert based on AlertDialog.Builder such a custom dialog does accept the getWindow().setSoftInputMode(...) command and therefore allows the soft keyboard to be displayed automatically.

    For guidance on building a custom dialog I found this web page and this especially helpful.

    0 讨论(0)
  • 2020-12-08 20:06

    Have you tried to set focus on your EditText -> inputBox.requestFocus() or something like that?

    0 讨论(0)
  • 2020-12-08 20:06

    If you want to pop up dialog box along with soft key pad, so that user could free from tap on edit text inside dialog to show keypad, for example if you are going to take some value from dialog box, then use this simple code, it will solve your problem.

            public void onClick(final View v) 
            {   
                 AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());                 
                  alert.setIcon(R.drawable.smsicon);
                  alert.setTitle(" Secrete Code");  
                  alert.setMessage("Enter a Key for secrete text !");
                  final EditText shft_val = new EditText(v.getContext()); 
                  shft_val.setInputType(InputType.TYPE_CLASS_NUMBER);//changing the keyBoard to No only,restrict the string etc
                  alert.setView(shft_val);
    
         //pOp Up the key pad on Edit Text  focus event
    
                 shft_val.setOnFocusChangeListener(new OnFocusChangeListener()
                 {
                    public void onFocusChange(View arg0, boolean arg1)
                    {  InputMethodManager inputMgr = (InputMethodManager)v.getContext().
                                        getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
                            }
                        });
    
                     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
                     {  
                     public void onClick(DialogInterface dialog, int whichButton) 
                     {
                        //Your specific code... 
                     }
                     });
                     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    
                         public void onClick(DialogInterface dialog, int which) {                       
                             dialog.dismiss();
                             return;   
                         }
                     });
                           alert.show();
                        }
    
    0 讨论(0)
  • 2020-12-08 20:10

    As long as you always need to show the keyboard immediately once the dialog opens rather than once a specific form widget inside gets focus (for instance, if your dialog just shows an EditText and a button), you can do the following:

    AlertDialog alertToShow = alert.create();
    alertToShow.getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    alertToShow.show();
    

    Rather than calling .show() on your builder immediately, you can instead call .create() which allows you to do some extra processing on it before you display it onto the screen.

    0 讨论(0)
提交回复
热议问题