吐司没有工作。选项卡中的微调框

时间:2015-07-29 03:57:10

标签: android tabs spinner android-toast

public static class SectionsFragment2 extends Fragment {
    public SectionsFragment2() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_page2, container, false);

        Spinner spinner_device = (Spinner) rootView.findViewById(R.id.spinner_device);

        ArrayAdapter<CharSequence> adapter_device = ArrayAdapter.createFromResource(getActivity(), R.array.array_device, android.R.layout.simple_spinner_item);

            adapter_device.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner_device.setAdapter(adapter_device);
            AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) {
                  Toast.makeText(parent.getContext(), "OnItemSelected: " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                }
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    //do nothing.
                }
            };
            return rootView;
        }
    }

吐司没有工作......

1 个答案:

答案 0 :(得分:0)

您没有将OnItemSelectedListener设置为Spinner。尝试添加以下行:

spinner_device.setOnItemSelectedListener(listener);