【问题标题】:How to handle NumberPicker Value change?如何处理 NumberPicker 值的变化?
【发布时间】:2018-08-02 00:26:31
【问题描述】:

我有一个包含游戏标题的NumberPicker。 问题是我希望每次有人滚动NumberPicker 并停在其中一个标题上时,我将能够立即处理此操作并用我想要的信息填写 2 EditText

在按钮按下时,每次在视图上按下Button 时都会处理void onClick(View view),我如何使用NumberPicker 做类似的事情?

这是我的代码:

NumberPicker picker;
SharedPreferences sp;
String JsonDataGL;
String[] arrayGL;
ProgressDialog p;
GamesLibrary[] gamesArray;
String Count = "0";
EditText gameTitleET, gameContentET;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_post);
    sp=getSharedPreferences("JsonData", 0);
    JsonDataGL = sp.getString("JsonGL", null);

    gameTitleET = findViewById(R.id.GameTitleET); //The 2 EditTexts I want to fill with text
    gameContentET = findViewById(R.id.GameTitleET);

    JSONObject json_data = null;
    try {
        json_data = new JSONObject(JsonDataGL);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        Count = json_data.getString("count");
        Log.d("Nugi32", Count);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    gamesArray = new GamesLibrary[Integer.parseInt(Count)];

    picker = (NumberPicker) findViewById(R.id.numberPicker2);
    picker.setMinValue(0);
    picker.setMaxValue(Integer.parseInt(Count)-1);
    arrayGL = new String[Integer.parseInt(Count)];
    DownLoadJsonGL downLoadJsonGL = new DownLoadJsonGL();
    downLoadJsonGL.execute(JsonDataGL); //Here The NumberPicker is filled with the titles

}

你知道我该怎么做吗?或引导我在线教程? 因为我在网上找不到与此主题相关的任何问题。 谢谢!

【问题讨论】:

  • 您必须为此创建自己的号码选择器。
  • 什么意思?
  • 好的,看来@ZeekHuge 有办法回答

标签: java android numberpicker


【解决方案1】:

您是否为此尝试过 on value change 监听器?当数字选择器中的值更改时将调用它。 https://developer.android.com/reference/android/widget/NumberPicker.OnValueChangeListener.html

void onValueChange (NumberPicker picker, 
                int oldVal, 
                int newVal)

【讨论】:

  • 您好,感谢您的帮助!这似乎正是我想要的,但我不知道如何检查我当前的 NumberPicker 值是否已通过此函数更改。
  • 它会给你新旧价值,我认为它也会检查只有在有实际变化时才调用这个方法。就像如果你连续不断地滚动并且它达到了比我认为它不会被调用的值。
【解决方案2】:

正如documentation for NumberPicker 所示, 你可以使用

picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal){
                //Process the changes here
            }
    });

【讨论】:

    猜你喜欢
    • 2019-02-14
    • 2012-11-10
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 2019-12-20
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多