当我点击按钮时,如何在一个编辑框上显示多个选定的微调项目数据

时间:2014-08-08 04:09:07

标签: android

当我选中多个微调项时,所有数据项都显示在一个单独的编辑框中。这是代码和xml的一部分。

 public void addItemsOnSpinner(){
        sp1=(Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.facility_array, android.R.layout.simple_spinner_item);   
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
        sp1.setAdapter(adapter);

    }
    public void onItemSelected(AdapterView<?> spinner, View arg1, int arg2,
                    long arg3) {

                // TODO Auto-generated method stub
                switch(view.getId()){
                //Spinner spinner1,spinner2,spinner3;
                case R.id.spinner1:
                    Toast.makeText(myetms.this,"spinner1",Toast.LENGTH_LONG).show();
                //break
                case R.id.spinner2:
                    Toast.makeText(myetms.this,"spinner2",Toast.LENGTH_LONG).show();
                //break;
                case R.id.spinner3:
                    Toast.makeText(myetms.this,"spinner3",Toast.LENGTH_LONG).show();
                break;
                }
            //sendSMS("9819861968","+location[index]");
                //sc.setVisibility(view.VISIBLE);       }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        };
        public void onItemSelected(AdapterView<?> sp1,View view,int pos,long id){
            //sc.setText((CharSequence) sp1.getSelectedItem());
    sc.setVisibility(view.VISIBLE);         
}

这是xml文件

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="132dp"
            android:src="@drawable/myshedule" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
                <TextView
                    android:id="@+id/TextView04"
                    android:layout_width="25dp"
                    android:layout_height="65dp"
                    android:layout_weight="1"
                    android:background="#dcdcdc"
                    android:gravity="center"
                    android:padding="20dip"
                    android:text="From"
                    android:textColor="#000000" />

                <EditText
                    android:id="@+id/editText"
                    android:layout_width="55dp"
                    android:layout_weight="1"
                    android:background="#d3d3d3"
                    android:gravity="center"
                    android:padding="20dip"
                    android:text="Select Date"
                    android:textColor="#000000" />

                <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_height="65dp"
                    android:contentDescription="@string/selectdate"
                    android:gravity="center"
                    android:padding="20dip"
                    android:src="@drawable/calendar_icon" />
            </TableRow>
            <TableRow
                android:id="@+id/tableRow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
                <TextView
                    android:layout_width="35dp"
                    android:layout_weight="1"
                    android:background="#dcdcdc"
                    android:gravity="center"
                    android:padding="20dip"
                    android:text="Emplyee Name"
                    android:textColor="#000000" >
                </TextView>

                <EditText
                    android:id="@+id/screenName"
                    android:layout_width="45dp"
                    android:layout_height="50dp"
                    android:layout_marginLeft="20dip"
                    android:layout_marginRight="20dip"
                    android:layout_weight="1" >
                </EditText>
            </TableRow>
            <TableRow
                android:id="@+id/tableRow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
                <TextView
                    android:layout_height="65dp"
                    android:layout_weight="1"
                    android:background="#b0b0b0"
                    android:gravity="center"
                    android:padding="18dip"
                    android:text="Facility Type"
                    android:textColor="#000000" />

                <Spinner
                    android:id="@+id/spinner1"
                    android:layout_height="65dp"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:entries="@array/facility_array"
                    android:gravity="center"
                    android:padding="18dip"
                    android:prompt="@string/facility_prompt"
                    android:textColor="#000000" />
            </TableRow>
            <TableRow
                android:id="@+id/tableRow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
                <TextView
                    android:layout_height="65dp"
                    android:layout_weight="1"
                    android:background="#b0b0b0"
                    android:gravity="center"
                    android:padding="18dip"
                    android:text="Trip Type"
                    android:textColor="#000000" />

                <Spinner
                    android:id="@+id/spinner2"
                    android:layout_height="65dp"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:entries="@array/trip_array"
                    android:gravity="center"
                    android:padding="18dip"
                    android:prompt="@string/trip_prompt"
                    android:textColor="#000000" />
            </TableRow>
            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
                <TextView
                    android:layout_height="65dp"
                    android:layout_weight="1"
                    android:background="#b0b0b0"
                    android:gravity="center"
                    android:padding="18dip"
                    android:text="Shift Type"
                    android:textColor="#000000" />

                <Spinner
                    android:id="@+id/spinner3"
                    android:layout_height="65dp"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:entries="@array/shift_array"
                    android:gravity="center"
                    android:padding="18dip"
                    android:prompt="@string/shift_prompt"
                    android:textColor="#000000" />
            </TableRow>
            <TableRow
                android:id="@+id/tableRow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
                <Button
                    android:id="@+id/btnSubmit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Submit" />
                <Button
                    android:id="@+id/btnSubmit1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Home" />
            </TableRow>
        </TableLayout>

        <EditText
            android:id="@+id/searchBox"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:inputType="textVisiblePassword"
            android:paddingLeft="10dp" >
        </EditText>
    </LinearLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:0)

试试这个:

全局定义String Variable,您需要在其中存储不同的Spinner Selected Values。

假设您有3个Spinners: 然后定义如:

String Spinner1Value,Spinner2Value,Spinner3Value;

之前

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
.....
}

现在每个Spinner setOnItemSelectedListener()

   spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> parent, View view, int position,
                            long id) {                                     
                       try
                       {
                        Spinner1Value= spinner1.getSelectedItem().toString();

                       }
                       catch(Exception e)
                       {
                           Toast.makeText(getBaseContext(), "Error::"+e.toString(), Toast.LENGTH_LONG).show();
                       }

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
                });  

之后,您可以从Spinner1Value,Spinner2Value,Spinner3Value中获取所选值。 像:

edittext1.setText(Spinner1Value+Spinner2Value+Spinner3Value);

希望这可以帮到你!