NumberPicker无法读取键盘的输入

时间:2014-05-23 08:29:49

标签: android numberpicker

作为标题,我打开一个数字选择器并想输入一个数字。但无论我输入什么,数字选择器都没有变化。它应该输入一个数字并使用该值来设置金额,但我无法更改其值。我该如何正确使用它?

这是我的代码:

private void loadAddBtnContent(){
        TextView add_to_cart = (TextView)view.findViewById(R.id.add_to_cart);
        if ( cart.checkInCart(pid) ){
            add_to_cart.setText("in Cart ("+cart.getProductAmount(pid)+")");
            add_to_cart.setOnClickListener(new OnClickListener(){               
                @Override
                public void onClick(View v) {                   
                    AlertDialog.Builder builder = new AlertDialog.Builder(pdActivity);
                    NumberPicker amountPicker = new NumberPicker(pdActivity);

                    builder.setView(amountPicker);
                    builder.setPositiveButton("Change", new addCartListener(amountPicker)).setNegativeButton("Cancel", null);
                    builder.show();
                }
            });
        } else {
            add_to_cart.setText("Add to cart");
            add_to_cart.setOnClickListener(new OnClickListener(){               
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(pdActivity);
                    NumberPicker amountPicker = new NumberPicker(pdActivity);
                    amountPicker.setMinValue(1);

                    builder.setView(amountPicker);
                    builder.setPositiveButton("Add", new addCartListener(amountPicker)).setNegativeButton("Cancel", null);
                    builder.show();
                }
            });
        }
    }

    private class addCartListener implements DialogInterface.OnClickListener{
        private NumberPicker amountPicker;

        public addCartListener(NumberPicker amountPicker){
            this.amountPicker = amountPicker;
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            switch(which){
            case DialogInterface.BUTTON_POSITIVE:
                amountPicker.clearFocus();
                cart.setInCartAmount(product, amountPicker.getValue());
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                break;
            }
            loadAddBtnContent();
        }

    }

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用其他设备模拟器

我遇到了与模拟器相同的问题,我更改了它并尝试了其他设备,它们都有效。