Displaying soft keyboard whenever AlertDialog.Builder object is opened

后端 未结 14 1764
半阙折子戏
半阙折子戏 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: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 = "
    \n"; html += "Google Search:
    \n"; html += "
    \n"; html += "
    \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"); }

提交回复
热议问题