用户输入导致表单出现

发布于 2024-11-16 18:25:16 字数 1134 浏览 0 评论 0原文

我希望当用户在微调器上选择“组合”时显示 EditText 对象,我该怎么做?

这是我一直在尝试的:

     ground = (Spinner) findViewById(R.id.ground);
    ArrayAdapter<CharSequence> groundAdapter = ArrayAdapter.createFromResource(
            this, R.array.ground_array, android.R.layout.simple_spinner_item);
    groundAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ground.setAdapter(groundAdapter);
    ground.setOnItemSelectedListener(new GroundListener());
    if(ground.getSelectedItem().toString().equalsIgnoreCase("Combination"))
    {
        combo.setVisibility(0);
    }

EditText 对象组合在 xml 文件中设置为 android:visibility="gone"

GroundListener 代码是

     public class GroundListener implements OnItemSelectedListener {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                String selected = parent.getItemAtPosition(pos).toString();
            }

            public void onNothingSelected(AdapterView parent)
            {
                // Do nothing.
            }
        }

I want to have EditText object appear when a the User chooses "Combination" on a Spinner, How would I do this?

Here is what I have been trying:

     ground = (Spinner) findViewById(R.id.ground);
    ArrayAdapter<CharSequence> groundAdapter = ArrayAdapter.createFromResource(
            this, R.array.ground_array, android.R.layout.simple_spinner_item);
    groundAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ground.setAdapter(groundAdapter);
    ground.setOnItemSelectedListener(new GroundListener());
    if(ground.getSelectedItem().toString().equalsIgnoreCase("Combination"))
    {
        combo.setVisibility(0);
    }

the EditText object combo is set in xml file as android:visibility="gone"

GroundListener Code is

     public class GroundListener implements OnItemSelectedListener {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                String selected = parent.getItemAtPosition(pos).toString();
            }

            public void onNothingSelected(AdapterView parent)
            {
                // Do nothing.
            }
        }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2024-11-23 18:25:16

什么是地面监听器?

您不应该使用 AdapterView.OnItemSelectedListener 及其 onItemSelected 方法吗?

此外,为了便于阅读,请使用 setVisibility(View.VISIBLE) 而不是 0。

编辑:

我不明白你在用你的代码做什么,你的 GroundListener 没有插入任何东西,你的测试在监听器之外。

尝试:

ground.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

           if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Combination"))
            {
              combo.setVisibility(View.VISIBLE);
            }
        }

        public void onNothingSelected(AdapterView parent)
        {
            // Do nothing.
        }
    });

检查是否有效,然后返回 GroundListener 中的代码以查看是否有效。但您可能会遇到问题,因为 GroundListener 可能不知道什么是组合。但你会解决这个问题的。

编辑:

语法更正

What is a GroundListener ?

Shouldn't you be using an AdapterView.OnItemSelectedListener with its onItemSelected method ?

Beside, use setVisibility(View.VISIBLE) instead of 0 for readability.

EDIT:

I don't understand what you are doing with your code, your GroundListener is not plugged to anything and your test is outside of the listener.

Try :

ground.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

           if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Combination"))
            {
              combo.setVisibility(View.VISIBLE);
            }
        }

        public void onNothingSelected(AdapterView parent)
        {
            // Do nothing.
        }
    });

Check if that works and then bring back the code in your GroundListener to see if it works. You might have a problem though with the fact that the GroundListener might not know what is combo. But you'll work that out.

Edit:

Syntax Correction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文