无法解决方法' getwindow()'在活动中

时间:2017-05-04 14:19:50

标签: java android

我正在尝试在我的活动builder.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

中添加此行

但我发现此错误无法解决方法' getwindow'

正在尝试添加我的代码

        NewGuestCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.this);
                builder.setTitle("Insert Table Name");
                // Set up the input
                final EditText input = new EditText(TabsActivity.this);
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_TEXT);
                builder.setView(input);
                // Set up the buttons
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final String Table_Name = input.getText().toString();
                        AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.this);
                        builder.setTitle("Insert number of Covers");
                        // Set up the input
                        final EditText input2 = new EditText(TabsActivity.this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
                        input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_NUMBER);
                        builder.setView(input2);
                        // Set up the buttons
                        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String Cover_check = input2.getText().toString();
                                TablesFragment.Check_Items = ConnectionClass.Ret_dt("Select * From ChecksItems Where Check_ID = 0");
                                if (!TablesFragment.Check_Items.containsKey("Change_Temp")) {
                                    if (TablesFragment.Check_Items.size() > 0) {
                                        ArrayList<Object> Valrows = new ArrayList<Object>();
                                        if (TablesFragment.Check_Items.get("Item_ID").size() > 0) {
                                            for (int i = 0; i < TablesFragment.Check_Items.get("Item_ID").size(); i++) {
                                                Valrows.add("");
                                            }
                                        }
                                        TablesFragment.Check_Items.put("Change_Temp", Valrows);
                                    }
                                }
                                if (Integer.parseInt(Cover_check) > 0) {
                                    String st = ConnectionClass.Ret_Col("Select  Max (CheckSerail) AS Ser From Checks_V where Officer = 0 AND OutLet_ID = " + ConnectionClass.OutletID + " And Rest_ID_Active = " + ConnectionClass.Rest_ID);
                                    if (st.trim() == "")
                                        st = "0";
                                    int Check_Serial = Integer.parseInt(st) + 1;
                                    long Check_ID = Long.parseLong(ConnectionClass.SelectNewIDCheck());
                                    st = "insert into Checks .......
                                }
                            }
                        });
                        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });

                        builder.show();
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
                builder.show();

因为我需要这种方法,当警报对话框启动时,键盘自动启动,我不需要触摸编辑文本来显示键盘 对不起,如果有任何事情不清楚,抱歉我的英语不好 我希望这个案子能够解决

通过活动看起来

public class TabsActivity extends AppCompatActivity {

3 个答案:

答案 0 :(得分:0)

通过提供任何视图reference.ex

来调用getWindow()
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

答案 1 :(得分:0)

StackOverflow上有类似的问题。您需要在getWindow()类上致电Dialog

答案 2 :(得分:0)

public static void showInputMethodManager(Context mContext, View view) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }

您可以在onClick()

中尝试以下代码

showInputMethodManager(TabsActivity.this)

之后调用方法builder.show();

如果它不起作用,请尝试this

AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

dialog.show();