【问题标题】:How to get selected text using android spinner wheel?如何使用 android 旋转轮获取选定的文本?
【发布时间】:2015-04-27 03:15:32
【问题描述】:

我正在使用Android Spinner Wheel,并且我已经为此设置了代码。

AbstractWheel wheelHorizontalView1 = (AbstractWheel) findViewById(R.id.wheelHorizontalView1);
NumericWheelAdapter minAdapter = new NumericWheelAdapter(this, 1, 15,"%01d");
minAdapter.setItemResource(R.layout.wheel_text_centered_dark_back);
minAdapter.setItemTextResource(R.id.text);
wheelHorizontalView1.setViewAdapter(minAdapter);

这是它的样子:

那么,我的问题是如何获得 3 作为我选择的文本?

如果需要,这里是xml,

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp">

    <View
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:background="#007704" />

    <com.spinnerwheel.WheelHorizontalView
        android:id="@+id/wheelHorizontalView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        app:selectionDivider="@android:color/white"
        app:selectionDividerWidth="2dp"
        app:visibleItems="6" />
</RelativeLayout>

【问题讨论】:

  • 您是否试图找出选择了哪个项目?因为您说“如何获得 3 作为我选择的文本?”,但 3 已经被选择,所以我想您的意思是“我如何确定选择的内容?”
  • 看起来想要获取选定的文本,因为问题是“如何获取选定的文本......”
  • 是的,我想获取所选文本的文本,滚动后出现绿色方块的任何字段。
  • 你是如何为选中的项目设置绿色背景的?

标签: java android spinner android-wheel


【解决方案1】:

你应该调用setCurrentItem(int index)函数:

wheelHorizontalView1.setCurrentItem(2);

如果您想知道用户何时更改了值 你必须实现这个监听器。

    wheelHorizontalView1.addChangingListener(changedListener);
    ...
    ...  
    // Wheel changed listener
    private OnWheelChangedListener changedListener = new OnWheelChangedListener() {
        public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
            // newValue is the currently selected item
        }
    };

【讨论】:

【解决方案2】:

这就是我在演示中发现的价值。

value=getWheel(viewId).getCurrentItem();

方法

   private AbstractWheel getWheel(int id) {
        return (AbstractWheel) findViewById(id);
    }

或者

value=wheelHorizontalView1.getCurrentItem();

【讨论】:

  • 任何特定的侦听器在那里,我可以调用 Toast.makeText(this, wheelHorizo​​ntalView.getCurrentItem()+1 + "", Toast.LENGTH_SHORT).show();因为它只在创建时调用一次。我希望它是动态的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 2015-04-02
  • 1970-01-01
相关资源
最近更新 更多