Android隐藏并显示numberPicker

时间:2014-10-21 15:33:31

标签: android show-hide numberpicker

当我点击可编辑的文本框时,我试图让NumberPicker出现,以便用户可以使用NumberPicker来选择值而不是键盘。

目前我只有:

 public void NumberPicker()
 {
     NumberPicker np = (NumberPicker)findViewById(R.id.np1);
     np.setMaxValue(300);
     np.setMinValue(0);
     np.setValue(0);
     np.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Toast.makeText(v.getContext(), "Number selected", Toast.LENGTH_SHORT).show();
        }
     });
  }

布局

 <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_alignParentTop="true" />

    <NumberPicker
        android:id="@+id/np1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

任何帮助都会很棒!

由于

1 个答案:

答案 0 :(得分:0)

        <TextView
            android:id="@+id/pid_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:gravity="center_horizontal"
            android:hint="IPD"
            android:inputType="numberSigned" />

        <NumberPicker
            android:id="@+id/pid_inputPicker"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:gravity="center_vertical|center_horizontal"
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/text_size_normal"
            android:visibility="gone" />

和代码

    String[] pidArray = {"54","56","58","60","62","64","66","68","70","72","74"};     

    pidInputPicker.setMinValue(0);
    pidInputPicker.setMaxValue(pidArray.length-1);
    pidInputPicker.setDisplayedValues(pidArray);
    pidInputPicker.setBackgroundColor(Color.WHITE);
    /* initially */
    pidInputPicker.setVisibility(View.GONE);
    pidInput.setVisibility(View.VISIBLE);


    pidInput.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pidInputPicker.setVisibility(View.VISIBLE);
        }
    });
    pidInputPicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String selected = pidArray[pidInputPicker.getValue()];
            pidInput.setText(selected);
            pidInputPicker.setVisibility(View.GONE);
        }
   }