How to add floating label on Spinner

前端 未结 10 1732
Happy的楠姐
Happy的楠姐 2020-12-12 14:46

After using the Android Design Support Library\'s TextInputLayout to place a floating label above an EditText component, I was wondering if there is a way to add a floating

10条回答
  •  心在旅途
    2020-12-12 15:20

    I have an alternative solution that uses the behavior of TextInputLayout and a custom DialogFragment (AlertDialog) to emulate a spinner dialog popup.

    layout.xml:

    
    
        
    
    

    Create Custom Spinner via DialogFragment (AlertDialog)

    SpinnerFragment.java:

    public class SpinnerFragment extends DialogFragment {
    
    private static final String TITLEID = "titleId";
    private static final String LISTID = "listId";
    private static final String EDITTEXTID = "editTextId";
    
    public static SpinnerFragment newInstance(int titleId, int listId, int editTextId) {
        Bundle bundle = new Bundle();
        bundle.putInt(TITLEID, titleId);
        bundle.putInt(LISTID, listId);
        bundle.putInt(EDITTEXTID, editTextId);
        SpinnerFragment spinnerFragment = new SpinnerFragment();
        spinnerFragment.setArguments(bundle);
    
        return spinnerFragment;
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final int titleId = getArguments().getInt(TITLEID);
        final int listId = getArguments().getInt(LISTID);
        final int editTextId = getArguments().getInt(EDITTEXTID);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
        try {
            final String[] items = getResources().getStringArray(listId);
    
            builder.setTitle(titleId)
                    .setItems(listId, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int pos) {
                            EditText et = (EditText) getActivity().findViewById(editTextId);
                            String selectedText = items[pos];
                            if (!TextUtils.isEmpty(selectedText)) {
                                et.setText(selectedText);
                            } else {
                                et.getText().clear();
                            }
                        }
                    });
    
        } catch (NullPointerException e) {
            Log.e(getClass().toString(), "Failed to select option in " + getActivity().toString() + " as there are no references for passed in resource Ids in Bundle", e);
            Toast.makeText(getActivity(), getString(R.string.error_failed_to_select), Toast.LENGTH_LONG).show();
        }
    
        return builder.create();
    }
    

    }

    Activity.java:

    private void addCustomSpinner() {
        EditText yourEt = (EditText) findViewById(R.id.your_et);
        yourEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCustomSpinnerDialog(view);
            }
        });
    }
    
    private void showCustomSpinnerDialog(View v) {
        int titleId = R.string.your_label;
        int listId = R.array.spinner_selections;
        int editTextId = R.id.your_et;
        SpinnerFragment spinnerFragment = SpinnerFragment.newInstance(titleId, listId, editTextId);
        spinnerFragment.show(getFragmentManager(), "customSpinner");
    }
    

    Result

    When you click on the spinner styled TextInputLayout, it will trigger an alert dialog containing your list of selections. Once a selection is chosen, the EditText will be populated with your selection and the label will float like how you want.

提交回复
热议问题