[英]how to start another activity when click on item from spinner items
I have spinner which including many items and want when I click on one item from those items it open another activity 我有一个微调器,其中包含许多项目,并且想要在我单击这些项目中的一个项目时打开另一个活动
here is the spinner in layout 这是布局中的微调器
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/title"
android:entries="@array/items"
/>
here is the items in string 这是字符串中的项目
<string name="title">select</string>
<string-array name="items">
<item>open activity one</item>
<item>open activity two</item>
</string-array>
here is the code that i want to make it able to open another activity when i click on the items 这是我要使其能够在单击项目时打开另一个活动的代码
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
}
});
try this: 尝试这个:
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
}
});
public class Main2Activity extends Activity { 公共类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;
// and so on // ..... // 等等 // .....
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
} }
you already have the onItemSelected
method. 您已经具有onItemSelected
方法。 So just put a switch-case statement in there with arg2 as argument. 因此,只需在其中放置一个带有arg2作为参数的switch-case语句即可。 arg2 is the position of the item. arg2是项目的位置。 so just put your intent to open another activity in the case you need. 因此,只要您有需要,就打算打开另一个活动。 cheers 干杯
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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.