0

我正在我的活动课程中以编程方式创建一个微调器。“微调器”看起来像下拉菜单,而不是微调器。我想我希望它看起来更像一个选择器(即日期选择器/时间选择器/数字选择器),您可以在其中旋转所有文本选项。

如果有可用的文本选择器,我会使用选择器类型的小部件,但我似乎找不到这样的小部件 - 只有一个数字选择器。这是我在活动中使用的代码。

        ArrayAdapter <String>lv1List = new ArrayAdapter<String>(this.getApplicationContext(),android.R.layout.simple_spinner_item,  new String[]{"item 1","item 2","item 3"});
        Spinner sp = new Spinner(getApplicationContext());
        sp.setAdapter(lv1List);
        sp.setOnItemSelectedListener(this);
4

2 回答 2

1

看看Android Wheel。你可以有文字或几乎任何东西。完美运行。

于 2012-09-04T19:24:59.347 回答
0

如果您想创建自己的微调器,则快速(且效率低下)示例。(与列表视图适配器一起使用):

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
        android:orientation="horizontal">

    <FrameLayout android:id="@+id/container"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:layout_gravity="center_vertical"
              />

    <LinearLayout android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_vertical"
                  android:orientation="vertical">
        <Button android:id="@+id/btn_up"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="up"/>
        <Button android:id="@+id/btn_down"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="down"/>
    </LinearLayout>

</LinearLayout>

CustomSpinner 类:

public class CustomSpinner extends FrameLayout{
// ------------------------------ FIELDS ------------------------------

    private FrameLayout mContainer;
    private SpinnerAdapter mAdapter;
    private int index = 0;

// --------------------------- CONSTRUCTORS ---------------------------

    public CustomSpinner(Context context) {
        super(context);
        init(context);
    }

    public CustomSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context){
        this.addView(LayoutInflater.from(context).inflate(R.layout.custom_spinner,this,false));

        findViewById(R.id.btn_up).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                index--;
                refresh();
            }
        });

        findViewById(R.id.btn_down).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                index++;
                refresh();
            }
        });

        mContainer = (FrameLayout) findViewById(R.id.container);
    }

// -------------------------- OTHER METHODS --------------------------

    public void setAdapter(SpinnerAdapter adapter) {
        this.mAdapter = adapter;
        refresh();
    }

    private void refresh() {
        //----needs recycling for better performance---
        //---now, we'll just clear up--
         mContainer.removeAllViews();

        //---do we need to refresh ? -----
        if(mAdapter == null || mAdapter.getCount() == 0){return;}

        //--clamp index--
        index = Math.max(0,index);
        index = Math.min(mAdapter.getCount() - 1, index);

        //--get view and show it-----
        View currentView = mAdapter.getView(index,null,mContainer);
         mContainer.addView(currentView);
    }
}

用例:

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
        >

 <com.example.CustomSpinner android:id="@+id/custom_spinner"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
                               />
</LinearLayout>

活动:

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
                new String[]{"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"});

        ((CustomSpinner)findViewById(R.id.custom_spinner)).setAdapter(adapter);

    }
}
于 2012-09-04T20:26:46.680 回答