简体   繁体   English

单击微调器项目中的项目时如何启动另一个活动

[英]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.

相关问题 在android中点击另一个Spinner的项目更改Spinner的项目 - Change items of a Spinner on item click of another Spinner in android 从微调器将数组项传递到另一个活动 - Passing an array item to another activity, from a spinner 当用户从另一个活动中单击“返回”时,启动新活动 - Start new activity when user click “Back” from another activity android在单击列表视图中的项目时启动另一个活动 - android start another activity on click on item in a listview 当您单击微调器中的项目并传递数据时,如何打开新活动,是否有可能我们无法通过微调器打开新活动? - how to open new activity when you click an item in spinner and pass data, and is it possible we cant open new activity through spinner? 当我们单击列表视图项时如何启动新活动 - How to start a new activity when we click on listview item 如何从项目单击NavDrawer中启动新活动 - How to start a new activity from item click in NavDrawer 如何从微调器的项目中调用活动 - How to call activity from the items of the spinner 在另一个活动中单击项目时从列表视图中获取id - getting the id from the listview when click on item , in another activity 如何将一项活动中的项目从一项活动发送到另一项 - How to send Items from one activity to another on item checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM