Popinner OnItem选择不起作用的PopUp窗口

时间:2017-01-10 07:28:21

标签: java android android-spinner popupwindow

我尝试在弹出窗口中实现spinner,选择spinnerOnItemselection无效,我在这种情况下有两种实验测试。

实验测试-1 我实施了OnItemSelectionListener,实验测试-1的结果是

  

java.lang.NullPointerException:尝试调用虚方法' void   android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'   在null对象引用上。

实验测试-2 使用getSelectedItem()方法获取位置,仅在Toast中显示第一个位置值的结果。

这是我的代码,

Java代码实现

private void DialogTest(){

                String[] Title =  {"Select Title","Teacher","Employer","Lecturer","Coach","Mentor","Co-Worker","Prinicpal","Dean","Other"};
                LayoutInflater layoutInflater =(LayoutInflater)getActivity().getBaseContext().getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.dialog_test, null);
                final PopupWindow popupWindow = new PopupWindow(popupView, isPortrait ? getHeightByPercentage(50) : getWidthByPercentage(50), LinearLayout.LayoutParams.WRAP_CONTENT);

                Spinner popupSpinner = (Spinner)popupView.findViewById(R.id.popupspinner);
                Button button_test_cancel =(Button)popupView.findViewById(R.id.button_test_cancel);
                Button button_test_send =(Button)popupView.findViewById(R.id.button_test_send);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, Title);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                popupSpinner.setAdapter(adapter);
                popupWindow.showAsDropDown(btn_request_testimonial, 50, -30);

        //Step-1 Experimental Test
                popupSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        int selectedPosition = position;
                        Toast.makeText(getActivity().getBaseContext(),selectedPosition, Toast.LENGTH_LONG).show();


                    }
                });

        //Step-2 Experimental Test
                final String categoryTitle = popupSpinner.getSelectedItem().toString();

                Toast.makeText(getActivity().getBaseContext(),categoryTitle, Toast.LENGTH_LONG).show();


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

                    }
                });

                button_test_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);

            }

dialog_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">
    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="38dp"
                    android:background="@color/colorAccent">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/testimonial_request"
                  android:textSize="14sp"
                  android:textStyle="bold"
        android:textColor="@color/white"
        android:layout_centerInParent="true"/>
    </RelativeLayout>

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                  android:padding="16dp">


        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp">

            <EditText
                android:id="@+id/edTxt_Testi_Name"
                style="@style/edittextstyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Name"
                android:inputType="textPersonName"
                android:textColor="@color/colorPrimaryDark"
                android:textColorHint="@color/colorAccent"
                tools:ignore="HardcodedText"/>
            <!--android:text="Trd@sm20"-->
        </android.support.design.widget.TextInputLayout>

        <Spinner
            android:id="@+id/popupspinner"
            style="@style/edittextstyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:spinnerMode="dialog"
            android:textColor="@color/colorPrimaryDark"
            android:textColorHint="@color/colorAccent"/>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp">

            <EditText
                android:id="@+id/edTxt_Testi_Email"
                style="@style/edittextstyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="E-Mail"
                android:inputType="textEmailAddress"
                android:textColor="@color/colorPrimaryDark"
                android:textColorHint="@color/colorAccent"
                tools:ignore="HardcodedText"/>
            <!--android:text="Trd@sm20"-->
        </android.support.design.widget.TextInputLayout>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp">

            <EditText
                android:id="@+id/edTxt_Testi_Org"
                style="@style/edittextstyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Organization Name"
                android:inputType="textPersonName"
                android:textColor="@color/colorPrimaryDark"
                android:textColorHint="@color/colorAccent"
                tools:ignore="HardcodedText"/>
            <!--android:text="Trd@sm20"-->
        </android.support.design.widget.TextInputLayout>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp">

            <EditText
                android:id="@+id/edTxt_Testi_Comments"
                style="@style/edittextstyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Comments"
                android:inputType="textMultiLine"
                android:textColor="@color/colorPrimaryDark"
                android:textColorHint="@color/colorAccent"
                tools:ignore="HardcodedText"/>
            <!--android:text="Trd@sm20"-->
        </android.support.design.widget.TextInputLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/button_test_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true" android:layout_alignParentTop="true"
                android:background="@color/colorAccent"
                android:text="Send"
                android:textColor="@color/white"
                android:textSize="12sp"/>
            <Button
                android:id="@+id/button_test_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/button_test_send"
                android:layout_marginEnd="6dp"
                android:layout_marginRight="6dp"
                android:layout_toLeftOf="@+id/button_test_send" android:layout_toStartOf="@+id/button_test_send"
                android:background="@color/colorAccent"
                android:text="Cancel"
                android:textColor="@color/white"
                android:textSize="12sp"/>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您将onClickListener设置为spinner,因此第一项被点击了。请你使用onItemSelectedListener

//Step-1 Experimental Test
        popupSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {

                        Toast.makeText(SolveTest.this, String.valueOf(position), Toast.LENGTH_LONG).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }

    });

答案 1 :(得分:0)

尝试这个

    @Override
     public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // TODO Auto-generated method stub
        Object item = arg0.getItemAtPosition(arg2);
        if (item!=null) {
            Toast.makeText(MainActivity.this, item.toString(),
                    Toast.LENGTH_SHORT).show();
        }
        Toast.makeText(MainActivity.this, "Selected",
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});