簡體   English   中英

MainActivity中彈出窗口中的AutoCompleteTextView

[英]AutoCompleteTextView within popup in MainActivity

在MainActivity中,我彈出帶有AutoCompleteTextView的窗口,它可以正常工作。 我什至可以為此做一些工作(egtextView.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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM