簡體   English   中英

微調項導致活動(Android Studio)

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

我從幾個月前開始編程,而stackoverflow始終是解決我的問題的好地方。 所以我的代碼越來越好,但是現在我要再次需要您的幫助。

編程:在我的App中,您可以從微調器中選擇項目,然后轉到下一頁,依此類推。 您必須從多個微調器中進行選擇,直到獲得結果...

一些代碼預覽(代碼有效,但是我現在不得不添加一些操作,而且我不知道如何...

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

因此,在此應用程序的狀態下,您可以從微調器中選擇字符串,然后如果按Botton 3,它將變為類Maulstellung

我的問題:我希望當選擇字符串“ a”時,它在按下按鈕3后進入頁面xy,而當選擇字符串“ b”時,它在按下按鈕3后進入頁面xyz,...,依此類推每個字符串...

希望你能幫我,對我英語不好

也許這個布局文件有幫助

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

我將以這種方式:

不確定這是您所需要的。

在全局聲明String selectedValueSpinner mySpinner (在String[] koerperform之后添加)。

刪除mySpinner旁邊的Spinner

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

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