2

我有微调器,其中包括许多项目,并且当我单击这些项目中的一个项目时,它会打开另一个活动

这是布局中的微调器

<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
        }

    });
4

4 回答 4

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

    }

});
于 2013-11-11T20:10:25.700 回答
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

        }

    });


}

}

于 2017-03-21T17:23:30.223 回答
0

你已经有了onItemSelected方法。因此,只需在其中放置一个 switch-case 语句,并以 arg2 作为参数。arg2 是项目的位置。所以只要把你的意图放在你需要的情况下打开另一个活动。干杯

于 2013-11-11T19:58:12.417 回答
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);
        }
    });
于 2018-04-08T17:25:31.190 回答