以编程方式使用edittext的android微调器

时间:2015-08-31 13:41:57

标签: android spinner

我需要在我的应用程序中有一个微调器,并支持用户输入新选项。 有谁知道如何在Android上支持它。

1 个答案:

答案 0 :(得分:2)

下面的代码表示带有静态 [ADD MORE] 元素的Spinner,当选择该元素时,代码会调用InputBox制作的自定义DialogFragment {1}}然后返回输入的文本并将其广告到适配器项目。

<强> 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

    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

<?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>