如何使微调器工作?

时间:2015-11-13 01:00:55

标签: android android-spinner

enter image description here嘿伙计我是旋转器的新手,由于某种原因,当我点击旋转器时,我无法获得下拉菜单。我是否需要在onItemSelected方法中包含一些东西才能下拉?

这是一些基本代码。

public class MyActivity extends Activity implements View.OnClickListener
{
    private Spinner dropDownspinner;
    private ArrayAdapter menuAdapter;
  @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
         //spinner
        dropDownspinner = (Spinner) findViewById(R.id.spinner);

        menuAdapter.createFromResource(this, R.array.Drop_Down_Menu, android.R.layout.simple_spinner_item);

        dropDownspinner.setAdapter(menuAdapter);

        dropDownspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
   }
}

STRING ARRAY:

<!--Main Spinner-->
<string name="Profile">Profile</string>
<string name="Friends">Friends </string>
<string name="Folders">Folders</string>
<string name="Edit">Setting</string>
<string-array name="Drop_Down_Menu">
    <item></item>
</string-array>

在我的布局中。

    <Spinner
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/spinner"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/textView6"
        android:layout_toStartOf="@+id/textView6" />

2 个答案:

答案 0 :(得分:3)

  

我是否需要在onItemSelected方法中包含一些东西才能下拉?

不,但您必须致电setDropDownViewResource()上的SpinnerAdapter来说明下拉条目的外观:

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
*/

package com.commonsware.android.selection;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerDemo extends Activity
  implements AdapterView.OnItemSelectedListener {
  private TextView selection;
  private static final String[] items={"lorem", "ipsum", "dolor",
          "sit", "amet",
          "consectetuer", "adipiscing", "elit", "morbi", "vel",
          "ligula", "vitae", "arcu", "aliquet", "mollis",
          "etiam", "vel", "erat", "placerat", "ante",
          "porttitor", "sodales", "pellentesque", "augue", "purus"};

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection=(TextView)findViewById(R.id.selection);

    Spinner spin=(Spinner)findViewById(R.id.spinner);
    spin.setOnItemSelectedListener(this);

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

    aa.setDropDownViewResource(
      android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(aa);
  }

  @Override
  public void onItemSelected(AdapterView<?> parent,
                                View v, int position, long id) {
    selection.setText(items[position]);
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {
    selection.setText("");
  }
}

答案 1 :(得分:2)

您需要添加微调器function,例如。 addItemsOnSpinner();中的onCreateView。然后添加

public void addItemsOnSpinner() {

        List<String> list = new ArrayList<String>();
        list.add("Profiles");
        list.add("Friends");
        list.add("Folder");
        list.add("Setting");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);


    }