Android Spinner在发布时不选择任何内容

发布于 2025-02-06 07:10:35 字数 3333 浏览 3 评论 0 原文

我基本上希望当用户检查复选框时旋转器出现(它起作用)。并且旋转器具有供用户选择的项目,但我知道Spinner在启动时会自动选择第一个项目。

- 问题 - :每当我检查复选框和旋转器时,我都可以按第一个项目,但是旋转器不会消失,我无法获得所选项目的字符串文本(因为我使用它设置复选框的文本)。我只能在第一个项目之后选择这些项目,然后在以下内容时:检查并取消选中框,我只能选择其他选项,而在此之前选择的选项则不相同。

- 目标 - :如果使用用户选择复选框时,可以在出现复选框时选择第一个项目,并将字符串文本设置在复选框上,即使在用户取消选中并选中了复选框之后,也能够选择同一项目。

复选框和旋转器的代码:

 //checkbox
        giftwrapchecker = findViewById(R.id.checker);
        notEligible();
        giftwrapchecker.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b==false){giftwrapoptions.setVisibility(View.GONE); pricetagTv.setText("RM " + (bottles * perbottle)); giftwrapchecker.setText(R.string.giftq);}
                else {giftwrapoptions.setVisibility(View.VISIBLE);setGiftprice();}
            }
        });

        //spinner to check if the user has interacted with the spinner yet
        giftwrapoptions.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                isTouched = true;
                return false;
            }
        });


        //getting user selection and removes spinner once user has chosen and also set the chosen item's text onto the checkbox

        giftwrapoptions.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //this is the first item that the spinner auto selects
            String preselecteditem = giftwrapoptions.getSelectedItem().toString();
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (isTouched) {
                    //this is the item that user has selected
                    String item =  giftwrapoptions.getSelectedItem().toString();
                    System.out.println("Selected "+item);
                    giftwrapchecker.setText(item);
                    giftwrapoptions.setVisibility(View.GONE);

                    if (item==preselecteditem){
                        giftwrapchecker.setText(preselecteditem);
                        giftwrapoptions.setVisibility(View.GONE);}

                    }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {}
        });

    }

    private int getSum() {
        int sum = 0;
        for (int i=0;i< qty.size();i++){
            sum += (Integer.parseInt(qty.get(i)));
        }
        return sum;
    }
  
    private void notEligible(){
        if (bottles==3 || bottles==6 || bottles==12){
            giftwrapchecker.setEnabled(true);
            giftwrapoptions.setEnabled(true);
           
        } else{
            giftwrapchecker.setChecked(false);
            giftwrapchecker.setEnabled(false);
            giftwrapchecker.setText(R.string.giftna);
            giftwrapoptions.setVisibility(View.GONE);
            giftwrapoptions.setEnabled(false);
        }
        pricetagTv.setText("RM " + (bottles * perbottle));
    }

I basically want the spinner to appear when the user checks the checkbox (it works). And the spinner has items for the user to pick but I know that spinner selects the first item automatically when launched.

-- Issue -- : whenever I check the checkbox and spinner appears, I can press the first item BUT the spinner does not GO AWAY and I can't get the String text of the selected item (as I use it to set the text of the CheckBox). I can only selected the items after the first one and then, WHEN: I check and uncheck the box I can only select other options and not the same one when selected before.

-- Goal -- : to be able to select the first item when checkbox appears if user selects it, and set the String text onto the Checkbox AND be able to select the same item even after the user unchecks and checks the checkbox.

Code for both checkbox and spinner:

 //checkbox
        giftwrapchecker = findViewById(R.id.checker);
        notEligible();
        giftwrapchecker.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b==false){giftwrapoptions.setVisibility(View.GONE); pricetagTv.setText("RM " + (bottles * perbottle)); giftwrapchecker.setText(R.string.giftq);}
                else {giftwrapoptions.setVisibility(View.VISIBLE);setGiftprice();}
            }
        });

        //spinner to check if the user has interacted with the spinner yet
        giftwrapoptions.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                isTouched = true;
                return false;
            }
        });


        //getting user selection and removes spinner once user has chosen and also set the chosen item's text onto the checkbox

        giftwrapoptions.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //this is the first item that the spinner auto selects
            String preselecteditem = giftwrapoptions.getSelectedItem().toString();
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (isTouched) {
                    //this is the item that user has selected
                    String item =  giftwrapoptions.getSelectedItem().toString();
                    System.out.println("Selected "+item);
                    giftwrapchecker.setText(item);
                    giftwrapoptions.setVisibility(View.GONE);

                    if (item==preselecteditem){
                        giftwrapchecker.setText(preselecteditem);
                        giftwrapoptions.setVisibility(View.GONE);}

                    }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {}
        });

    }

    private int getSum() {
        int sum = 0;
        for (int i=0;i< qty.size();i++){
            sum += (Integer.parseInt(qty.get(i)));
        }
        return sum;
    }
  
    private void notEligible(){
        if (bottles==3 || bottles==6 || bottles==12){
            giftwrapchecker.setEnabled(true);
            giftwrapoptions.setEnabled(true);
           
        } else{
            giftwrapchecker.setChecked(false);
            giftwrapchecker.setEnabled(false);
            giftwrapchecker.setText(R.string.giftna);
            giftwrapoptions.setVisibility(View.GONE);
            giftwrapoptions.setEnabled(false);
        }
        pricetagTv.setText("RM " + (bottles * perbottle));
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文