每个项目启动不同活动的微调器?

时间:2013-11-12 09:16:03

标签: android android-activity spinner launch

我有一个带有项目的微调器,当我点击其中一个项目时,我想要它打开另一个活动

MainActivity布局中的微调器

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/imageView1"
    android:entries="@array/spinner1"            
     />

strings.xml中的Spinner

<string-array name="spinner1">
   <item>NEWS</item>
   <item>RESULTS</item>
   <item>CLASIFICATION</item>             
</string-array>

我想将“RESULTS”链接到Results.java,将“CLASIFICATION”链接到Clasification.java;我应该使用什么代码以及在哪里?感谢

5 个答案:

答案 0 :(得分:1)

    youspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1,
                int position, long arg3) {
    if(position == requiredposion)
        {
            Intent i = new Intent();
            i.setClass(CurrentActivity.this, NextActivity.class);
            startActivity(i);
        }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

            }
        });

答案 1 :(得分:0)

您可以向您的Spinners添加OnItemSelectedListener,然后您可以从中获取所选数据。

然后,您可以根据所选数据导航到每个活动。

 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View arg1,
        int position, long arg3) {

    String data = spinnerDataSource.get(position); 
    if(data.equals("RESULTS")) {
     //navigates to result class
    }else if(data.equals("CLASIFICATION")) {
      //navigates to classification class 
    }else {
      //navigates to News class
    }


    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
});

答案 2 :(得分:0)

您可以使用以下代码: -

Spinner s = (Spinner)findViewbyId(R.id.spinner1);
s.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View v,
                int pos, long arg3) {

            switch (pos) {
            case 0:
                  Intent intent = new Intent(MainActivity.this,News.class);
                      startActivity(intent);

                break;
            case 1:
                  //similar code here like above
                break;
            case 2:
                break;
            default:
                break;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

答案 3 :(得分:0)

试试如下:

  spin.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            System.err.println("**************" + arg2);

            switch (arg2) {
            case 0:
                 Intent i = new Intent();
                                i.setClass(CurrentActivity.this, NEWS.class);
                                 startActivity(i);
                break;
                               case 1:
                 Intent ir = new Intent();
                                ir.setClass(CurrentActivity.this, Results.class);
                                 startActivity(ir);
                break;
                               case 2:
                 Intent ic = new Intent();
                                ic.setClass(CurrentActivity.this, Clasification.class);
                                 startActivity(ic);
                break;
                     }}
                   @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

答案 4 :(得分:0)

试试这个......

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            TextView textView = (TextView) view;
            String text = textView.getText().toString().toLowerCase(Locale.getDefault());
            text = Character.toUpperCase(text.charAt(0))+""+text.substring(1);
            try {
                Class<?> cls = Class.forName("yourPackageName."+text);
                Intent  intent = new Intent(SecondActivity.this, cls);
                startActivity(intent);
            } catch (Exception e) {
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });