如何从自定义微调器获取所选文本?
我有旋转器包含名称Raju和Rani;如果我选择“Raju”,那么“Raju”必须打印,如果我选择“Rani”,那么“Rani”必须打印。
spinnerName = (Spinner)m_view.findViewById(R.id.spinner_name);
ArrayAdapter<String> adapterName = new ArrayAdapter<String>(MainActivity.this, 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()));
String spinnertext = spinnerCity.getSelectedItem().toString();
System.out.println("spinner" +spinnertext);
}
return v;
}
@Override
public int getCount() {
return super.getCount()-1; // you dont display last item. It is used as hint.
}
};
adapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterCity.add("Raju");
adapterCity.add("Rani");
adapterCity.add("name"); //This is the text that will be displayed as hint.
spinnerName.setAdapter(adapterName);
spinnerName.setSelection(adapterName.getCount());
答案 0 :(得分:1)
我认为你已经填充了你的微调器。现在编写此代码以从spinner获取所选文本
spinnerName.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String text = spinnerName.getSelectedItem().toString();
}
}
文本包含所选文本..现在您可以将其用作逻辑。 希望它有效...
答案 1 :(得分:0)
使用setOnItemSelectedListener。它将返回微调器的选定项
实施例
spinner.setOnItemSelectedListener(this);
...
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}