java - MainActivity 中弹出窗口中的 AutoCompleteTextView

标签 java android popup

在 MainActivity 中,我弹出了 AutoCompleteTextView 并且它可以工作。我什至可以用这个来做一些工作(例如textView.setText(“New”))。但我担心适配器,因为单击 TextView 后没有任何反应(没有列表和键盘)。

我认为这个问题是符合的:

ArrayAdapter<String> adapter = new ArrayAdapter <String (customView.getContext(),android.R.layout.simple_dropdown_item_1line, countryNameList);

特别是在第一个参数 - 上下文中。我不知道我应该在那里放什么。 我的代码:

public void steptwo() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View customView = inflater.inflate(R.layout.popup_observedproperty,null);

        mPopupWindow = new PopupWindow(
                customView,
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );

        String[] countryNameList = {"India", "China", "Australia", "New Zealand", "England", "Pakistan"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(customView.getContext(),
                android.R.layout.simple_dropdown_item_1line, countryNameList);
        AutoCompleteTextView textView = (AutoCompleteTextView) customView.findViewById(R.id.autoCompleteTextView);
        textView.setText("New");
        textView.setAdapter(adapter);

        if(Build.VERSION.SDK_INT>=21){
            mPopupWindow.setElevation(5.0f);
        }

        mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
    }

最佳答案

您需要使 PopupWindow 可聚焦。要允许 AutoCompleteTextView 立即打开键盘,请将 PopupWindowSoftInputMode 设置为 SOFT_INPUT_STATE_ALWAYS_VISIBLE

public void steptwo() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View customView = inflater.inflate(R.layout.popup_observedproperty,null);

        mPopupWindow = new PopupWindow(
                customView,
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );

        String[] countryNameList = {"India", "China", "Australia", "New Zealand", "England", "Pakistan"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(customView.getContext(),
                android.R.layout.simple_dropdown_item_1line, countryNameList);
        AutoCompleteTextView textView = (AutoCompleteTextView) customView.findViewById(R.id.autoCompleteTextView);
        textView.setText("New");
        textView.setAdapter(adapter);
        textView.setThreshold(1);

        if(Build.VERSION.SDK_INT>=21){
            mPopupWindow.setElevation(5.0f);
        }
        mPopupWindow.setFocusable(true);

        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
    }

关于java - MainActivity 中弹出窗口中的 AutoCompleteTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56988982/

相关文章:

java - 如何在Java中指定网络接口(interface)

java - 我的代码线程安全吗?

android - 更新recyclerview中的值

android - 适用于盲人的无障碍 Android Air 应用程序。使用 Android 的 TalkBack

keyboard - iOS 8 : Keyboard Extension. 添加弹出键的问题

java - ZonedDateTime 与 MongoDB

java - Integer.parseInt() 问题

java - Android:在 Switch 语句中嵌套 for 循环以获取 ArrayList 的总和

Android弹出窗体强制关闭

javascript - 页面加载后弹出而不是按钮