【发布时间】:2014-08-23 20:11:18
【问题描述】:
我在使用 android NumberPicker 时遇到问题。我相信这是因为 onValueChange(NumberPicker picker, int oldVal, int newVal) 调用。我目前在代码中没有 oldVal 或 newVal ...我的问题是如何在不使用 TextView xml 对象的情况下保存 NumberPicker 中的值。目前我无法在 NumberPicker 上滚动回较低的数字,我假设这是因为 onValueChange 方法......我不想在屏幕上显示当前或以前选择的数字,所以我想避免在其中设置 TextView xml,但我不确定如何保存数字选择器的当前值...
java 文件:
package com.example.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
static final int MIN_VAL = 0;
static final int MAX_VAL = 255;
NumberPicker red, green, blue;
View v;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red = (NumberPicker) findViewById(R.id.redPicker);
green = (NumberPicker) findViewById(R.id.greenPicker);
blue = (NumberPicker) findViewById(R.id.bluePicker);
red.setMinValue(MIN_VAL);
red.setMaxValue(MAX_VAL);
red.setWrapSelectorWheel(false);
green.setMinValue(MIN_VAL);
green.setMaxValue(MAX_VAL);
green.setWrapSelectorWheel(false);
blue.setMinValue(MIN_VAL);
blue.setMaxValue(MAX_VAL);
blue.setWrapSelectorWheel(false);
v = findViewById(R.id.color_box);
v.setBackgroundColor(0x0000FF00);
red.setOnValueChangedListener(new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// TODO Auto-generated method stub
v.setBackgroundColor(0xFF11F000);
}
});
}
}
xml 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="187dp">
<NumberPicker
android:id="@+id/redPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<NumberPicker
android:id="@+id/greenPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<NumberPicker
android:id="@+id/bluePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</LinearLayout>
<View
android:id="@+id/color_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
【问题讨论】:
-
“我无法滚动回较小的数字”是什么意思
-
我可以在数字选择器上向上滚动,但是当我尝试选择较小的数字时,它会冻结一秒钟,然后选择较大的数字。
-
你在 onValueChanged() 上做了什么。你能发布那个方法吗?
-
它贴在 java 文件的底部。但这是我的问题,我不知道在那里做什么。我想要做的是保存选择器中的数字......我现在所拥有的只是它改变了视图的背景颜色。
-
--保存新号码
标签: java android numberpicker