设置Android微调器以导航到另一个页面

时间:2013-12-14 17:05:46

标签: android android-activity navigation spinner

我是Android应用程序的新手。

我有一个微调器设置,会有很多不同的选项。

你能帮帮我吗?

我需要使用每个不同的微调器选项打开一个新页面。

我该怎么做?

代码:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Spinner
  android:id="@+id/spinner1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="30dp"
  android:entries="@array/model_numbers"
  android:prompt="@string/model_spinner" />

<TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:text="Please select model number below:"
  android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
  android:id="@+id/textView2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:text="peter.gosling@email.com"
  android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
  android:id="@+id/textView3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_above="@+id/textView2"
  android:layout_centerHorizontal="true"
  android:layout_marginBottom="5dp"
  android:text="Version 1.0"
  android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

MainActivity.java

package com.example.capitaokipartslist;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
   getMenuInflater().inflate(R.menu.main, menu);
   return true;
}

}

的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Capita Oki Parts List</string>
<string name="action_settings">Settings</string>
<string name="model_number">Please select your model number:</string>
<string name="model_spinner">Please select your model number:</string>
<string-array name="model_numbers">
  <item >C711</item>
  <item >ES7411</item>
</string-array>

</resources>

提前感谢您提供的任何帮助!

2 个答案:

答案 0 :(得分:1)

请抛出这些教程

http://www.mkyong.com/android/android-spinner-drop-down-list-example/

http://androidexample.com/Spinner_Basics_-_Android_Example/index.php?view=article_discription&aid=82&aaid=105

http://androidituts.com/android-spinner-tutorial/

在onItemSelected方法中传递意图

Intent i=new Intent(CurrentActivity.this,Myclass.class);
startActivity(i);

<强>更新

当您要求继续进行不同的活动时,您可以使用if或切换应用条件并相应地移动。

例如: -

 public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(), 
    "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
    Toast.LENGTH_SHORT).show();
switch(pos)
                {
                    case 0:
                        Intent i=new Intent(CurrentActivity.this,Myclass1.class);
    startActivity(i);
                        break;
                    case 1:
                        Intent i=new Intent(CurrentActivity.this,Myclass2.class);
    startActivity(i);
                }

  }

此处CurrentActivity是您要导航的当前活动名称,Myclass1和Myclass2是您要去的那个类。

还有其他方法可以帮助您:

parent.getItemAtPosition(pos).toString()

答案 1 :(得分:0)

你必须在onCreate方法中沿着这些方面做点什么;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final String [] array = getResources().getStringArray(R.array.model_numbers);
        spin = (Spinner) findViewById(R.id.spinner);
        spin.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.a_layout, array));
        spin.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View view, int position, long id)
            {
                switch(position)
                {
                    case 0:
                        startActivity(new Intent(MainActivity.this, NewClass.class));
                        break;
                    case 1:
                        startActivity(new Intent(MainActivity.this, OtherClass.class));
                        break;
                }
            }

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

            }
        });
    }