Android开发-使微调器看起来像微调器



我正在活动类中以编程方式创建一个微调器。"微调器"看起来像是一个下拉列表,而不是一个微调器。我想我希望它看起来更像一个选择器(即日期选择器/时间选择器/数字选择器),在那里你可以旋转所有的文本选项。

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

        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);

看看Android Wheel。你可以在上面写文字或几乎任何东西。效果很好。

如果您想创建自己的微调器,请使用快速(低效)示例。(适用于列表视图适配器):

布局:

<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);
    }
}

相关内容

最新更新