首页 > 解决方案 > 数字选择器数据未正确显示android

问题描述

大家好,我遇到了号码选择器的问题。我有一些字符串需要显示为 在此处输入图像描述

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

在此处输入图像描述

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

主要活动

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>

对话框.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 本机问题。请帮我解决这个问题。

标签: androidnumberpickerandroid-number-picker

解决方案


我已经尝试了许多解决方法来修复它,比如扩展类,其中有一个函数 getSelectedPos,它返回函数 start 的基础上的位置,而不是 equals 或 equalignorecase。但由于是 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 上设置的给定数组中的任何字符串的子字符串。


推荐阅读