我有旋转器,其中包含许多项目,并且当我点击这些项目中的一个项目时,它会打开另一项活动
这是布局中的微调器
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/title"
android:entries="@array/items"
/>
这是字符串
中的项目<string name="title">select</string>
<string-array name="items">
<item>open activity one</item>
<item>open activity two</item>
</string-array>
这里是我希望能够在点击项目
时打开另一个活动的代码Spinner Spinner = (Spinner) findViewById(R.id.spinner);
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:3)
试试这个:
Spinner Spinner = (Spinner) findViewById(R.id.spinner);
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long row_id) {
final Intent intent;
switch(position){
case 1:
intent = new Intent(CurrentActivity.this, TargetActivity1.class);
break;
case 2:
intent = new Intent(CurrentActivity.this, TargetActivity2.class);
break;
// and so on
// .....
}
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:1)
公共类Main2Activity扩展了Activity {
Toolbar mytoolbar;
Spinner mySpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mytoolbar=(Toolbar) findViewById(R.id.toolbar);
mySpinner=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdaptor=new ArrayAdapter<String>(Main2Activity.this,
R.layout.custom_spinner_item,
getResources().getStringArray(R.array.names));
myAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdaptor);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long row_id)
{
final Intent intent;
switch(position)
{
case 1:
intent = new Intent(Main2Activity.this, MainhomeActivity.class);
startActivity(intent);
break;
case 2:
intent = new Intent(Main2Activity.this, DateActivity.class);
startActivity(intent);
break;
//等等 // .....
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
答案 2 :(得分:0)
您已经拥有onItemSelected
方法。所以只需在其中放置一个switch-case语句,并将arg2作为参数。 arg2是项目的位置。所以只是打算在你需要的情况下打开另一个活动。欢呼声
答案 3 :(得分:0)
String selection ;
acTV1.setAdapter(arrayAdapter);
acTV1.setCursorVisible(false);
acTV1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
acTV1.showDropDown();
String selection = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), selection, Toast.LENGTH_SHORT);
if(selection.equals("Delete"))
{
intent = new Intent(ImageAttachmentActivity.this, DeleteEmployeeActivity.class);
startActivity(intent);
}
});