号码选择器从相反方向增加号码

时间:2018-12-05 17:22:55

标签: java android xamarin

现在,我的号码选择器开始将我的号码从上到下递增。我可以将其更改为相反的方向吗?

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="25px"
    android:minHeight="25px"
    android:id="@+id/numberPicker1" />
</RelativeLayout>

活动

val numberPicker = (NumberPicker)FindViewById(Resource.Id.numberPicker1);
numberPicker.MaxValue = 100;
numberPicker.WrapSelectorWheel = false;

请参见下图:

enter image description here

1 个答案:

答案 0 :(得分:0)

解决方案:

您可以自己定义订单。使用SetDisplayedValues将具有字符串值的数组设置为numberPicker。然后numberPicker的显示值将与数组中的值顺序具有相同的顺序。

例如:

如果定义数组 String[] numbers = new string[] { "10", "9", "8", "7", "6", "5", "4", "3", "2","1" };, 然后设置numberPicker.SetDisplayedValues(numbers);numberPicker.Value=1;,显示值将从10到1。

这是您的解决方案:

NumberPicker numberPicker = (NumberPicker)FindViewById(Resource.Id.numberPicker1);

//define your own array here
//String[] numbers = new string[] { "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "0-" };

ArrayList list = new ArrayList();

for (int i = 100; i > 0; i--)
{
      list.Add (Convert.ToString(i));
}

string[] numbers = (string[])list.ToArray(typeof(string));

numberPicker.SetDisplayedValues(numbers);
//set the min value
numberPicker.MinValue=1;
numberPicker.MaxValue=numbers.Length;
//set the default value
numberPicker.Value=1;    
numberPicker.WrapSelectorWheel = false;