可以在 Android 应用程序中以编程方式打开 Spinner 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2679804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:53:42  来源:igfitidea点击:

Possible to programmatically open a Spinner in Android app?

android

提问by JohnRock

If you have a handle to a Spinnerobject in an Android Activity, can you programmatically pop open the spinner options - thereby forcing the user to choose an option even though they did not click on the Spinnerthemselves?

如果您在 Android Activity 中有一个Spinner对象的句柄,您能否以编程方式弹出打开 Spinner 选项 - 从而迫使用户选择一个选项,即使他们没有点击Spinner自己?

回答by Tim Kryger

To open the Spinner you just need to call it's performClick()method.

要打开 Spinner,您只需调用它的performClick()方法。

Keep in mind that you may only call this method from the UI thread. If you need to open the Spinner from a separate thread you should create a Handlerin the UI thread and then, from your second thread, send a runnable object that calls performClick() to the Handler.

请记住,您只能从 UI 线程调用此方法。如果您需要从单独的线程打开微调器,您应该在 UI 线程中创建一个处理程序,然后从您的第二个线程发送一个调用 performClick() 的可运行对象到处理程序。

package com.example.SpinnerDemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.os.Handler;

public class SpinnerDemo extends Activity {

    private Handler h;
    private Spinner s;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        h = new Handler();

        s = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
                R.array.planets, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);

        // Open the Spinner...
        s.performClick();

        // Spawn a thread that triggers the Spinner to open after 5 seconds...
        new Thread(new Runnable() {
            public void run() {
                // DO NOT ATTEMPT TO DIRECTLY UPDATE THE UI HERE, IT WON'T WORK!
                // YOU MUST POST THE WORK TO THE UI THREAD'S HANDLER
                h.postDelayed(new Runnable() {
                    public void run() {
                        // Open the Spinner...
                        s.performClick();
                    }
                }, 5000);
            }
        }).start();
    }
}

The resources used by this example can be found here.

可以在此处找到此示例使用的资源。

回答by Amintabar

To show the Spinneritems you just need to call it's performClick()method.

要显示Spinner您只需要调用它的performClick()方法的项目。

Spinner spDeviceType = (Spinner) findViewById(R.id.spDeviceType);
spDeviceType.performClick();

回答by Kovalenych

You don't need to use 2 runnables as shown in the previous example.

您不需要使用前一个示例中所示的 2 个可运行对象。

This will be enough :

这就足够了:

h.postDelayed(new Runnable() {
    public void run() {
        s.performClick();
    }
}, 5000);

回答by Chayon Ahmed

Simply use this

简单地使用这个

yourspinner.performClick();