【问题标题】:Number picker data is not showing correctly android数字选择器数据未正确显示android
【发布时间】:2019-01-29 12:08:18
【问题描述】:

大家好,我遇到了号码选择器的问题。我有一些字符串需要显示为

但是在使用选择按钮选择第二个选项“菜单”并重新打开包含数字选择器的对话框时,数据显示如下

在这里可以看到菜单 1 显示在数字选择器选项菜单的上方和中间的菜单中。但是在滚动时会显示正确的数据。代码如下

MainActivity

package com.example.anakumar6.numberpickerexample;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.NumberPicker;


public class MainActivity extends AppCompatActivity {
    int index =0;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Get the widgets reference from XML layout
        tv = (TextView) findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(MainActivity.this);
            }
        });

    }

    public void showDialog(Activity activity){
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.dialog);

        final NumberPicker np = (NumberPicker) dialog.findViewById(R.id.np);

        //Set TextView text color
        tv.setTextColor(Color.parseColor("#FF2C834F"));

        //Initializing a new string array with elements
        final String[] values= {"Menu 1", "Menu", "Menu 2"};
//        modifyDataForNumberPicker(values);


        //Populate NumberPicker values from String array values
        //Set the minimum value of NumberPicker
        np.setMinValue(0); //from array first value
        //Specify the maximum value/number of NumberPicker
        np.setMaxValue(values.length-1); //to array last value

        //Specify the NumberPicker data source as array elements
        np.setDisplayedValues(values);
        np.setValue(index);

        //Gets whether the selector wheel wraps when reaching the min/max value.
        np.setWrapSelectorWheel(true);

        //Set a value change listener for NumberPicker
        np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal){
                //Display the newly selected value from picker
                tv.setText("Selected value : " + values[newVal]);
                index = newVal;
            }
        });

        Button dialogButton = (Button) dialog.findViewById(R.id.btnClick);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:padding="16dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select a number..."
        android:textSize="25dp" />
</RelativeLayout>

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffffff"
    >

    <NumberPicker
        android:id="@+id/np"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_below="@id/tv"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnClick"
        android:text="select"/>
</LinearLayout>

我认为这是 android number picker 本机问题。请帮我解决这个问题。

【问题讨论】:

  • 你能正确说出问题吗?
  • 为了获得更多帮助,我附上了图片。正如我们所看到的,在第一张图片中有三个选项可用,我选择了第二个选项“菜单”,然后重新打开对话框,我看到菜单 1 显示在中间一个菜单和上一个菜单上,这是不正确的。但是在滚动正确的数据时,显示的数据与正确的第一个屏幕相同。简而言之,在重新打开数字选择器对话框时,会显示不正确的数据,但在与数字选择器交互时会显示正确的数据。
  • 好的,我知道了,检查一下
  • 我运行了您的代码,但无法获得任何解决方案。我认为这是本机类中的拼写错误,因此当我更改数组的顺序时,它可以正常工作。如果可能的话,请更改菜单,菜单1,菜单2等顺序。

标签: android numberpicker android-number-picker


【解决方案1】:

我已经尝试了许多解决方法来修复它,比如扩展类,其中有一个函数 getSelectedPos,它返回以函数开头而不是等于或等于或等于或等于大小写为基础的位置。但由于是 NumberPicker 类的私有方法,它无法完成。我还尝试了其他无效的解决方法。

最后我为这个问题找到了一个简单的解决方法。我创建了自己的函数来修改要在 NumberPicker 上设置的数据,然后再在 NumberPicker 上设置。

modifyDataForNumberPicker(values);
np.setDisplayedValues(values);

private void modifyDataForNumberPicker(String[] dataList){
        int i=0;
        for(String data : dataList){
            int pos = i++;
            dataList[pos] = data+" ";
        }
    } 

此函数可防止任何字符串成为 NumberPicker 上要设置的给定数组中任何字符串的子字符串。

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多