设置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>

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

请扔这些教程

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或switch应用条件并相应地移动。

例如:-

 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是您想要导航的当前Activity名称,Myclass1和Myclass2是您想要去的那个类。

还有另一种方法可以帮助你喜欢:

parent.getItemAtPosition(pos).toString()

您必须在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)
            {
            }
        });
    }

相关内容

  • 没有找到相关文章

最新更新