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

时间:2015-09-08 10:09:35

标签: android android-spinner android-adapter custom-adapter

我以编程方式创建了三个微调器。我只想选择一个微调项目值,其他微调器将默认值设置为“select”。

实施例: 如果用户选择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选择了项目,那么下次用户无法为spinner2选择相同位置的项目,反之亦然。

1 个答案:

答案 0 :(得分:1)

首先,我将向您展示最终结果(API 17):

Photos(我没有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>

现在是Spinner:

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"

使用R.array.yourArray,YourEditText和String在OnCreate中调用此函数,以显示为任何Spinners的Title。