当我从微调器中选择时,如何使用按钮开始新活动? 我的代码...我已经搜索了很多但没有任何作用,希望这里发生的事情:)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton button = (ImageButton) findViewById(R.id.imgbtnarroceros);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
Spinner spinner;
spinner= (Spinner) findViewById(R.id.spinner) ;
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.itinerary, android.R.layout.simple_spinner_item) ;
spinner.setAdapter(adapter);
答案 0 :(得分:2)
根据我的理解,您希望在从微调器中选择项目时打开一个具有按钮的活动。如果我的理解是错误的,请纠正我。
setOnItemSelectedListener到您的微调器并处理项目选择事件。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
//start activity on selection of any item you want, here I am assuming first item.
Intent intent = new Intent(YourCurrentActivity.this, ActivityWithButton.class);
startActivity(intent);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});