Java 是否可以在 Android 中设置提示 Spinner
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6602339/
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
Is it possibile to set hint Spinner in Android
提问by Nick
Is there anyway of making a hint for a spinner similar to hint that is provided for edit text fields. I know you can use a prompt that gives you a title bar but still leaves the initial spinner field blank until you click into the spinner. I currently have a crude way of setting a dummy field as the first part of the spinner array which is the question and then have a check at the end to make sure the spinner doesn't equal the question string. Is there any cleaner / better way of doing this?
是否有任何提示类似于为编辑文本字段提供的提示的微调器。我知道您可以使用一个提示,为您提供一个标题栏,但在您单击进入微调器之前仍将初始微调器字段留空。我目前有一种粗略的方法来设置一个虚拟字段作为问题的微调器数组的第一部分,然后在最后进行检查以确保微调器不等于问题字符串。有没有更清洁/更好的方法来做到这一点?
Thanks!
谢谢!
采纳答案by Boni2k
Here's a solution which is probably a bit simpler than Ravi Vyas code (thanks for the inspiration!):
这是一个可能比 Ravi Vyas 代码简单一点的解决方案(感谢您的启发!):
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView)v.findViewById(android.R.id.text1)).setText("");
((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
}
return v;
}
@Override
public int getCount() {
return super.getCount()-1; // you dont display last item. It is used as hint.
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("Item 1");
adapter.add("Item 2");
adapter.add("Hint to be displayed");
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //display hint
回答by Ravi Vyas
回答by SBerg413
An even easier way than setting up your own spinner adapter is to just use a button and style it like a spinner object with
比设置您自己的微调适配器更简单的方法是只使用一个按钮并将其设置为带有微调对象的样式
android:background="@android:drawable/btn_dropdown"
Then setup the button's onClick event to open a single-item-select dialog. You can then do whatever you want with the text of the button.
然后设置按钮的 onClick 事件以打开单项选择对话框。然后你可以对按钮的文本做任何你想做的事情。
This has been my preferred way of handling this for a while. Hope it helps someone.
一段时间以来,这一直是我首选的处理方式。希望它可以帮助某人。
EDIT:I've been playing around with this again recently (and someone asked me to post an example a while ago). This strategy will look a bit different if you're using the Holo theme. However, if you're using other themes such as Theme.Black, this will look identical.
编辑:我最近又在玩这个(不久前有人让我发布一个例子)。如果您使用 Holo 主题,此策略看起来会有所不同。但是,如果您使用其他主题,例如 Theme.Black,这看起来是相同的。
To demonstrate this, I made a simple app that has both a regular Spinner along with my custom button-spinner. I threw this up in a GitHub repo, but here's what the Activity looks like:
为了证明这一点,我制作了一个简单的应用程序,它既有常规的 Spinner 又有我的自定义按钮微调器。我把它放到了一个GitHub repo 中,但这是 Activity 的样子:
package com.stevebergamini.spinnerbutton;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner spinner1;
Button button1;
AlertDialog ad;
String[] countries;
int selected = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
button1 = (Button) findViewById(R.id.button1);
countries = getResources().getStringArray(R.array.country_names);
// You can also use an adapter for the allert dialog if you'd like
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countries);
ad = new AlertDialog.Builder(MainActivity.this).setSingleChoiceItems(countries, selected,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
button1.setText(countries[which]);
selected = which;
ad.dismiss();
}}).setTitle(R.string.select_country).create();
button1.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
ad.getListView().setSelection(selected);
ad.show();
}});
}
}