可以以编程方式打开Android应用一个微调? [英] Possible to programmatically open a Spinner in Android app?
问题描述
如果你有一个手柄,在Android 活动
A 微调
的对象,可以以编程方式弹出打开微调选项 - 从而迫使用户选择的选项,即使他们没有点击微调
自己?
If you have a handle to a Spinner
object 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 spinner
themselves?
推荐答案
要打开微调你只需要调用它的<一个href="http://developer.android.com/reference/android/widget/Spinner.html#performClick%28%29">performClick()方法。
To open the Spinner you just need to call it's performClick() method.
请记住,你只能叫从UI线程此方法。如果你需要从一个单独的线程打开微调应该创建一个处理器在UI线程然后,从你的第二个线程,发送调用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 Handler in the UI thread and then, from your second thread, send a runnable object that calls performClick() to the Handler.
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.
这篇关于可以以编程方式打开Android应用一个微调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!