如何处理多个微调器的选定项目事件?

How to handle selected item event of multiple spinners?

我以编程方式创建了三个微调器。我只希望一个微调项的值应该 selected,其他微调项的默认值将设置为“select”。

示例: 如果用户 selects 来自 spinner1 的项目,那么 spinner2spinner3 将具有默认值“Select”并继续。

为了事件处理,编写了这个自定义侦听器:

    OnExItemSelectedListener onItemSelectedListner =  new OnExItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) {
            Log.e("selected","test");
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            Log.e("nothing","test");
        }

        @Override
        public void typeSelected(Spinner customSpinner,int type,int position) {

            if (type == 1) {

                if(spinner1 !=null)
                {
                    spinner1 .setSelection(position);
                    spinner2 .setSelection(-1);
                    spinner3 .setSelection(-1);
                }

                if(spinner1Txt!=null)
                spinner1Txt.setText( ""+customSpinner.getItemAtPosition(position));

                if(spinner2Txt!=null)
                spinner2Txt.setText("Select card");
                if(spinner3Txt!=null)
                spinner3Txt.setText("Select card");
            }
            else if (type == 2) {

                if(spinner2 !=null)
                {
                    spinner2 .setSelection(position);
                    spinner1 .setSelection(-1);
                    spinner3 .setSelection(-1);
                }
                    if(spinner2Txt!=null)
                spinner2Txt.setText( ""+customSpinner.getItemAtPosition(position));

                if(spinner1Txt!=null)
                spinner1Txt.setText("Select card");
                if(spinner3Txt!=null)
                spinner3Txt.setText("Select card");

            }
            else if (type == 3) {
                if(spinner3 !=null)
                {
                    spinner3 .setSelection(position);
                    spinner2 .setSelection(-1);
                    spinner1 .setSelection(-1);
                }
                if(spinner3Txt!=null)
                    spinner3Txt.setText("Select card");
                    if(spinner2Txt!=null)
                    spinner2Txt.setText("Select card");
                    if(spinner1Txt!=null)
                    spinner1Txt.setText(""+customSpinner.getItemAtPosition(position));
            }
        }
    };

上面代码的问题是:如果用户在 spinner1 的位置 0 编辑了项目 select,那么下次用户无法在 spinner2 的相同位置 select 项目,反之亦然。

首先我会告诉你最终结果(API 17):

Photos(我没有 post 图片的 10 声望 :D)

现在让我们创建自定义列表视图 (list_view.xml):

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:gravity="center">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/ListTitle"
              android:layout_width="fill_parent"
              android:textSize="20sp"
              android:layout_height="50dp"
              android:layout_marginLeft="20dp"
              android:textColor="@color/dialog_text"
              android:gravity="center_vertical"
              android:textStyle="bold" />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/border"
              android:layout_below="@id/ListTitle"
              android:layout_width="fill_parent"
              android:layout_height="3dp" 
              android:background="yourColor" />

    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_below="@id/border"
                  android:gravity="center"
                  android:orientation="horizontal" >

        <ListView android:id="@+id/ListView" 
                  android:layout_height="wrap_content"
                  android:layout_width="fill_parent" android:listSelector="color for item hover/click" android:scrollbars="none" />

    </LinearLayout>

</RelativeLayout>

现在微调器:

private void Spinner(int array, final EditText field, String setTitle) {

    String[] items = getResources().getStringArray(array);

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.list_view, null);
    TextView mytitle = (TextView) view.findViewById(R.id.ListTitle);
    mytitle.setText(setTitle);

    ListView listView = (ListView) view.findViewById(R.id.ListView);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

    listView.setAdapter(adapter);

    final AlertDialog spinner = new AlertDialog.Builder(this)
            .setView(view)
            .create();

    field.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            spinner.show();

        }

    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String item = (String) parent.getItemAtPosition(position); // Selected item

            field.setText(item); // This sets the selected item as value for its EditText

            spinner2.setText("something"); //The value you want to display on spinner2 
            spinner3.setText("something"); //The value you want to display on spinner3 

        }
    });

}

现在如何使用它:

在微调器的 EditText 中添加 android:focusableInTouchMode="false"

并在您的 OnCreate 中使用 R.array.yourArray、YourEditText 和字符串调用此函数以显示为您的任何 Spinner 的标题。