如何在微调器之间传递选定的微调器项目?

编程入门 行业动态 更新时间:2024-10-25 16:30:44
本文介绍了如何在微调器之间传递选定的微调器项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

经过详尽的搜索和大量的思考,我无法在AndroidStudio中找到以下问题的解决方案:

After a thorough search and quite a lot of thinking, I couldn't find a solution to the following problem in AndroidStudio:

我有2个微调器(输入和输出).我想将输入微调器的值传递给选择输出微调器的值(onItemSelected)时调用的方法.有关代码段如下:

I have 2 spinners (input and output). I want to pass the value of the input spinner to a method that is called upon selection of a value of the output spinner (onItemSelected). The regarding code passage looks as follows:

private void setupSpinnerListeners() { spinnerLengthInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String itemSelectedInSpinnerLengthInput = parent.getItemAtPosition(position).toString(); checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); spinnerLengthOutput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String itemSelectedInSpinnerLengthOutput = parent.getItemAtPosition(position).toString(); updateOutputTextfield(itemSelectedInSpinnerLengthInput, itemSelectedInSpinnerLengthOutput); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); }

我希望在输出微调器的onItemSelected方法中可以使用String itemSelectedInSpinnerLengthInput(从输入微调器获取其值).如何做到这一点? 任何帮助,我们将不胜感激.

I want the String itemSelectedInSpinnerLengthInput (that gets its value from the input spinner) to be available in the onItemSelected method of the output spinner. How to accomplish this? Any help is greatly appreciated.

在setupSpinnerListeners方法内部创建一个全局变量,它是一个长度为1的数组.它将按预期工作.

Create a global variable INSIDE the setupSpinnerListeners Method, that is an array with length 1. The it'll work as I had intended.

推荐答案

我建议您使用 OnItemSelectedListener .

I recommend you to use OnItemSelectedListener.

然后创建一个globalVariable,将String移至您的第一个Spinner,如下所示:

Then create a globalVariable to get the String to your first Spinner as follows :

String FirstValue = "";

然后您需要调用此名称:

Then you'll need to call this :

spinnerLengthInput.setOnItemSelectedListener(this); spinnerLengthOutput.setOnItemSelectedListener(this);

当然,您需要implements OnItemSelectedListener

然后,您可以在内部执行与以前相同的操作.

Then inside you can do the same that you were doing.

@Override public void onItemSelected(AdapterView<?> spinner, View view, int position,long id) { FirstValue = spinner.getItemAtPosition(position).toString(); checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput); }

然后在其他Spinner中使用FirstValue值.

更多推荐

如何在微调器之间传递选定的微调器项目?

本文发布于:2023-10-09 06:28:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!