【问题标题】:Looking for a option spinner/option wheel like control in android在android中寻找一个选项微调器/选项轮,如控件
【发布时间】:2012-06-11 03:44:24
【问题描述】:

寻找一个控件,允许一次为 android 选择一个文本值,每侧有 2 个箭头。不确定控件的名称,但这里是一个示例图像:

【问题讨论】:

  • 我从未见过这样的控件。您可能只需要自己创建它。
  • 我在很多地方都看到过它,但我认为我没有在 android 中看到过。如果我没有得到答案,我会自己做。如果我没有的话,我只是想避免重新发明轮子(对不起双关语)。
  • 我应该用“for android”来限定我的答案。我也在别处见过。 :)

标签: android user-interface spinner


【解决方案1】:

如果是日期,我可能会建议您使用看起来像 this 的 DatePicker。否则,如果有时间的话,您可能想要this 之类的东西。

但是,如果您正在寻找类似 NumberPicker 的东西,它似乎只有 API 11 及更高版本可用。因此,如果您愿意,您可能需要自定义您自己的。

【讨论】:

  • 这些都不是。它仅适用于文本。 Android 中没有这样的控件,但那里可能有一个库/示例文本。我可能需要成功。
  • 嗯.. 还有一个微调器,就像一个下拉列表,每次可以选择一个,但我想您应该自己制作,因为您提供的示例图像看起来更好。
  • 在大多数情况下是的,我会使用它,但不会在这个中使用。我已经在 AlertDialog 中,在顶部创建另一个弹出窗口看起来会很乱。此外,我在每个微调器控件中只有 2 或 3 个选项,其中一些微调器不希望每个都有弹出窗口。但是感谢您的帮助:)
【解决方案2】:

嘿,我想看看this 轮控制.. 由第三方创建的自定义控件。他还在他的博客中提供了其他详细信息..您可以根据需要对其进行修改..

【讨论】:

  • 感谢您的回答。我确实看过,这是我想要的,但将其转换为水平需要做很多工作。
【解决方案3】:

创建了我自己的,这对我来说非常有用。厨房控制帮助很大。请随时指出任何问题。

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import nz.co.nuffie.android.crichq.R;

import java.util.ArrayList;
import java.util.List;

public class ScrollWheelControl extends LinearLayout{
    private final ArrayList<String> textList = new ArrayList<String>();
    private ArrayAdapter optionAdapter = null;
    private AdapterView.OnItemSelectedListener itemSelectedListener = null;

    public ScrollWheelControl(Context context){
        super(context);
    }

    public ScrollWheelControl(Context context, AttributeSet attrs){
        super(context, attrs);
        initView(context);
    }

    public ScrollWheelControl(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        initView(context);
    }

    public void initView(Context context) {
        View.inflate(context, R.layout.scroll_wheel_control, this);

        optionAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_gallery_item, textList )
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position,convertView,parent);
                if(v instanceof TextView){//Just in case.
                    TextView txt = (TextView)v;
                    Gallery.LayoutParams glp = new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT);
                    txt.setGravity(Gravity.CENTER);
                    txt.setLayoutParams(glp);
                }
                return v;
            }
        };


        optionAdapter.notifyDataSetChanged();

        final Gallery g = (Gallery) findViewById(R.id.gOptionSelect);
        g.setAdapter(optionAdapter);

        final Button btnLeft = (Button) findViewById(R.id.btnLeft);
        final Button btnRight = (Button) findViewById(R.id.btnRight);
        g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> list, View view, int position, long id) {
                if(position >= textList.size()-1){
                    btnRight.setTextColor(Color.GRAY);
                }else
                    btnRight.setTextColor(Color.WHITE);

                if(position == 0){
                    btnLeft.setTextColor(Color.GRAY);
                }else
                    btnLeft.setTextColor(Color.WHITE);

                if(itemSelectedListener != null){
                    itemSelectedListener.onItemSelected(list,view,position,id);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        btnLeft.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                g.onFling(null, null, 2000, 0);
            }
        });
        btnRight.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                g.onFling(null, null, -2000, 0);
            }
        });
    }

    public void setOnItemListener(AdapterView.OnItemSelectedListener setItemSelectedListener){
        itemSelectedListener = setItemSelectedListener;
    }

    public void addTextItems(List<String> list){
        textList.addAll(list);
        if(optionAdapter != null){
            optionAdapter.notifyDataSetChanged();
        }
    }
}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp">

    <Button
        android:id="@+id/btnLeft"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="\u003C"
        android:background="@android:color/transparent"
        android:textColor="@android:color/white"
        android:shadowColor="@android:color/black"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:textStyle="bold"
        android:minWidth="15dp"
            />

    <Gallery
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:id="@+id/gOptionSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:spacing="0dp">
    </Gallery>

    <Button
        android:id="@+id/btnRight"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="\u003E" 
        android:textColor="@android:color/white"
        android:shadowColor="@android:color/black"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:textStyle="bold"
        android:minWidth="15dp"
            />

</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    相关资源
    最近更新 更多