【问题标题】:Spinner which launch different activity with each item?Spinner 为每个项目启动不同的活动?
【发布时间】:2013-11-12 09:16:02
【问题描述】:

我有一个带有项目的微调器,我希望当我单击其中一个项目时,它会打开另一个活动

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 中的微调器

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

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

【问题讨论】:

    标签: android android-activity spinner launch


    【解决方案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
    
                }
            });
    

    【讨论】:

      【解决方案2】:

      尝试如下:

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

      【讨论】:

        【解决方案3】:

        你可以使用下面的代码:-

        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) {
        
                }
            });
        

        【讨论】:

          【解决方案4】:

          您可以将OnItemSelectedListener 添加到您的 Spinners,然后您可以从那里获取您选择的数据。

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

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

          【讨论】:

            【解决方案5】:

            试试这个...

            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) {
            
                    }
                });
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-04-02
              • 1970-01-01
              • 1970-01-01
              • 2015-12-21
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多