我尝试使用微调器控件结果,以便将其指向应用程序上的另一个屏幕。例如,在微调控件中,如果用户选择了雪佛兰,那么它会带你到另一个用chevy.xml和Chevy.class编码的屏幕。这是我到目前为止所需要的一些帮助,因为我们的书只使用了getSelectedItem,并且该示例仅用于输出语句。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
public class Mainpage extends Activity implements OnItemSelectedListener {
String carChoice, chevy, ford, dodge, toyota;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* carChoice = group.getSelectedItem().toString();
}
if (carChoice.compareTo(chevy)==0)
{
startActivity(new Intent(Mainpage.this, Chevy.class));
*/ }
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
final Spinner group = (Spinner) findViewById(R.id.carGroup);
group.setOnItemSelectedListener(this);
// TODO Auto-generated method stub
String selected = group.getItemAtPosition(1).toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
有关
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
arg2是所选项目的索引,arg3是所选项目的id。您可以调用getItem()方法来获取所选的项目对象。
答案 1 :(得分:0)
public class SpinnerExample extends Activity
{
Spinner sp;
ArrayAdapter<String> adapter;
String numbers[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
"EIGHT", "NINE", "TEN" };
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp = (Spinner) findViewById(R.id.spinner1);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, numbers);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
}
sp.getSelectedItem()。toString()获取项目保存在这里
答案 2 :(得分:0)
请参阅以下代码,了解Android中微调器的完整示例。
public class ArrayAdapterDemo2 extends Activity implements
AdapterView.OnItemSelectedListener {
TextView selection;
String[] items = { "Dipak", "Keshariya", "Software", "Deepak",
"Android Example", "Android Book",
"Android", "i-phone", "Developer", "Tutorial" };
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView) findViewById(R.id.selection);
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
items);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// Here Position is Item Index
selection.setText(spin.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
}
请参阅以下链接以获取更多信息。