简体   繁体   English

微调项导致活动(Android Studio)

[英]Spinner Item leads to Activity (Android Studio)

I started programming a few month ago and stackoverflow always was aa good dite for solving my problems. 我从几个月前开始编程,而stackoverflow始终是解决我的问题的好地方。 So my codes are getting better but now I´m on a point that I Need your help again. 所以我的代码越来越好,但是现在我要再次需要您的帮助。

Programm: In my App you can choose items from a spinner, then it goes to next page and so on. 编程:在我的App中,您可以从微调器中选择项目,然后转到下一页,依此类推。 You have to choose from several Spinners until you get a result... 您必须从多个微调器中进行选择,直到获得结果...

Some Code preview (Code works, but i now have to ad some more Action and I don´t know how... 一些代码预览(代码有效,但是我现在不得不添加一些操作,而且我不知道如何...

package com.sio.fmf;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Button;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.TextView;

public class Koerperform extends AppCompatActivity {
  String[] koerperform = {" ", "spindel- oder torpedoförmig", "langgestreckt", "hochrückig", "schlangenförmig", "welsartig", "grundelartig"};

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

    Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
    mySpinner.setAdapter(new MyCustomAdapter(Koerperform.this,R.layout.spinner_layout, koerperform));
  }

  public void onClick(View v){}
  public class MyCustomAdapter extends ArrayAdapter<String> {
    public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) {
      super(context, textViewResourceId, objects);
    }

    private Button getSwtitchact;
    {
      final Button switchact = (Button) findViewById(R.id.button3);
      switchact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent act = new Intent(view.getContext(), Maulstellung.class);
          startActivity(act);
        }
      });
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
      return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {
      LayoutInflater inflater = getLayoutInflater();
      View row = inflater.inflate(R.layout.spinner_layout, parent, false);
      TextView label = (TextView) row.findViewById(R.id.koerper);
      label.setText(koerperform[position]);

      if (position == 0) {
        label.setTextColor(0xFFF00000);
      }
      return row;
    }
  }
}

So in this state of the app you can choose the string from the spinner and then if you press Botton 3 it changes to class Maulstellung 因此,在此应用程序的状态下,您可以从微调器中选择字符串,然后如果按Botton 3,它将变为类Maulstellung

My Problem: I want that when string "a" is choosen it goes to page xy after Button 3 is pressed and when string "b" is choosen it goes to page xyz after Button 3 is pressed,..., and so on for each string... 我的问题:我希望当选择字符串“ a”时,它在按下按钮3后进入页面xy,而当选择字符串“ b”时,它在按下按钮3后进入页面xyz,...,依此类推每个字符串...

Hope you can help me and sorry for my bad English 希望你能帮我,对我英语不好

maybe this layout file helps 也许这个布局文件有帮助

<pre>
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".Koerperform"
    android:background="#023738">




    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:id="@+id/imageView1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/header" />

    <ImageView
        android:layout_width="400dp"
        android:layout_height="100dp"
        android:id="@+id/imageView5"
        android:background="@drawable/frageeins"
        android:layout_below="@+id/space4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:id="@+id/imageView6"
        android:background="@drawable/fischeins"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imageView5" />

    <Spinner
        android:id="@+id/spinner"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView6"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false"
        android:contextClickable="false"
        />

    <Button
        android:layout_width="220dp"
        android:layout_height="60dp"
        android:id="@+id/button3"
        android:layout_marginTop="69dp"
        android:background="@drawable/costum_button_weiter_gehts"
        android:layout_below="@+id/imageView6"
        android:layout_centerHorizontal="true" />

    <Space
        android:layout_width="30dp"
        android:layout_height="20dp"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:id="@+id/space4" />

</RelativeLayout>

I will do in this way: 我将以这种方式:

Not sure is this what you need. 不确定这是您所需要的。

Declare String selectedValue and Spinner mySpinner in global(add after line String[] koerperform ). 在全局声明String selectedValueSpinner mySpinner (在String[] koerperform之后添加)。

Remove Spinner beside mySpinner: 删除mySpinner旁边的Spinner

Spinner mySpinner = (Spinner)findViewById(R.id.spinner);

Inside getSwtitchact , change to this getSwtitchact内部,更改为此

  private Button getSwtitchact;
    {
      final Button switchact = (Button) findViewById(R.id.button3);
      switchact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          selectedValue=mySpinner.getSelectedItem().toString(); // here you get the selected items from spinner
          if(selectedValue.equals("a"))
          {
          Intent act = new Intent(view.getContext(), xy.class);
          startActivity(act);
          }
          else if(selectedValue.equals("B"))
          {
          Intent act = new Intent(view.getContext(), xyZ.class);
          startActivity(act);
          }
          else
         {
           ......
         }
        }
      });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM