2016-03-03 55 views
0

我幾個月前開始編程,並且stackoverflow總是解決我的問題的一個很好的選擇。所以我的代碼變得越來越好,但現在我覺得我需要你的幫助了。微調項目導致活動(Android Studio)

程序:在我的應用程序中,您可以從微調框中選擇項目,然後轉到下一頁等等。您可以從幾個紗廠選擇,直到你得到的結果...

一些代碼預覽(代碼工作,但我現在不得不廣告多一些行動和我不知道如何...

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

所以在應用程序的這種狀態下,您可以選擇從旋轉的字符串,然後如果按波頓3它Maulstellung

我的問題變成類:我想,當字符串「a」是choosen它去在按下按鈕3之後將頁面xy切換到頁面xy,並且當選擇字符串「b」時,按下按鈕3之後,轉到頁面xyz d,......,依此類推每串...

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

+0

多少微調,你呢? –

+0

嗨約翰,每個班級只有一個 –

+0

如果選擇字符串a,請轉到xy ... Maulstellung for? –

回答

0

我會這樣做:

不知道這是你所需要的。

在全局聲明String selectedValueSpinner mySpinner(在行String[] koerperform後面添加)。

刪除Spinner mySpinner旁邊:

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

嘿約翰,看起來像我想要的。多謝。當我再次回到家時,我會嘗試守則。什麼意思是「在全局聲明String selectedValue」? :O –

+0

在String [] koerperform'下面聲明'String selectedValue'。 –

+0

只是有一次嘗試,但仍有一點問題我不能解決codeline「selectedValue = mySpinner.getSelectedITem()等」有一個錯誤mySpinner無法解析,並在代碼中紅色?是什麼原因?對不起,如果我只是問愚蠢的問題 –

0

也許這個佈局文件幫助

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

相關問題