简体   繁体   English

android spinner与edittext以编程方式

[英]android spinner with edittext programmatically

I need to have a spinner in my application with support to enter new options by user. 我需要在应用程序中有一个微调器,以支持用户输入新选项。 Do anyone know how to support that on android. 有谁知道如何在android上支持它。

The code that follows represents a Spinner with an static [ADD MORE] element and when that element is chosen, the code calls for an custom InputBox made with DialogFragment and then returns the inputted text and ads it to the adapter items. DialogFragment的代码表示带有静态[ADD MORE]元素的Spinner ,选择该元素后,代码将调用使用DialogFragment的自定义InputBox ,然后返回输入的文本并将其投放到适配器项。

MainActivity.java MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements cAdditionals.myIBFragment.YesNoListener {

    @Override
    public void onInputBoxYes() {
        arr.remove("[ADD MORE]");
        arr.add(cAdditionals.myIBFragment.received_text);
        arr.add("[ADD MORE]");
        adapter = new elAdapter(this, arr);
        spnMain.setAdapter(adapter);
    }

    @Override
    public void onInputBoxNo() {
        //ON DISMISS
    }

    Spinner spnMain;
    ArrayAdapter<CharSequence> adapter;
    ArrayList<CharSequence> arr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spnMain = (Spinner) findViewById(R.id.spnMain);

        arr = new ArrayList<>();
        arr.add("[ADD MORE]");

        adapter = new elAdapter(this, arr);
        spnMain.setAdapter(adapter);
    }

    public class elAdapter extends ArrayAdapter<CharSequence> {
        private final ArrayList<CharSequence> ellist;
        private final Activity context;

        public elAdapter(Activity context, ArrayList<CharSequence> list) {
            super(context, R.layout.row, list);
            this.context = context;
            this.ellist = list;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.row, parent, false);

            TextView tvTitle = (TextView) rowView.findViewById(R.id.tvLabel);
            tvTitle.setText(ellist.get(position));

            return rowView;
        }

        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.row, parent, false);
            final TextView tv = (TextView) rowView.findViewById(R.id.tvLabel);
            tv.setText(ellist.get(position));

            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (tv.getText().toString().equals("[ADD MORE]")) {
                        cAdditionals.myIBFragment.message = "bla bla bla";
                        new cAdditionals.myIBFragment().show(getFragmentManager(), "tag");
                    }
                }
            });
            return rowView;
        }
    }
}

cAdditionals.java cAdditionals.java

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.text.InputFilter;
    import android.text.InputType;
    import android.text.method.DigitsKeyListener;
    import android.widget.EditText;

    /**
     * Created by Movsar Bekaev on 31/08/2015.
     */
    public  class cAdditionals {

        public static class myIBFragment extends DialogFragment {
            public static String message = "", received_text = "";

            public interface YesNoListener {
                void onInputBoxYes();
                void onInputBoxNo();
            }

            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
                if (!(activity instanceof YesNoListener)) {
                    throw new ClassCastException(activity.toString() + " must implement YesNoListener");
                }
            }

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                final EditText input = new EditText(getActivity());

                return new AlertDialog.Builder(getActivity())
                        .setTitle(message)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                received_text = input.getText().toString().trim();
                                ((YesNoListener) getActivity()).onInputBoxYes();
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ((YesNoListener) getActivity()).onInputBoxNo();
                            }
                        })
                        .setView(input)
                        .create();
            }

            @Override
            public void onCancel(DialogInterface dialog) {
                ((YesNoListener) getActivity()).onInputBoxNo();
                super.onCancel(dialog);
            }
        }
    }

row.xml row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="New Text"
        android:id="@+id/tvLabel"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM