两个旋转器,依靠一个旋转器进行阵列/输入

时间:2014-01-11 05:04:08

标签: android arrays

您好我正在制作一个转换器应用程序(就像您在谷歌“温度转换器”时那样)所以我有一个旋转器使另外两个旋转器显示相同的阵列。我知道那里有代码,但我没有找到一些处理许多不同的数组。例如,我需要一个Spinner能够使另外两个旋转器显示相同的阵列,但我有许多不同的阵列(温度/长度/质量/速度/体积/面积/时间)

的strings.xml

<string-array name="units">
    <item>Temperature</item>
    <item>Length</item>
    <item>Mass</item>
    <item>Speed</item>
    <item>Volume</item>
    <item>Area</item>
    <item>Time</item>    
   </string-array>

   <string-array name="Temperature">
    <item>Celsius</item>
    <item>Fahrenheit</item>     
   </string-array>


      <string-array name="Mass">
    <item>Metric Ton</item>
    <item>Kilogram</item>
    <item>Gram</item>
    <item>Milligram</item>
    <item>Pound</item>
    <item>Ounce</item>
   </string-array>

   <string-array name="Length">
    <item>Kilometer</item>
    <item>Meter</item>
    <item>Centimeter</item>
    <item>Millimeter</item>
    <item>Mile</item>
    <item>Yard</item>
    <item>Foot</item>
    <item>Inch</item>           
   </string-array>

   <string-array name="Speed">
    <item>Miles/Hour</item>
    <item>Feet/Sec</item>
    <item>Meters/Sec</item>
    <item>Km/Hour</item>
    <item>Knot</item>
   </string-array>

   <string-array name="Time">
    <item>Nanosecond</item>
    <item>Microsecond</item>
    <item>Meters/Sec</item>
    <item>Km/Hour</item>
    <item>Knot</item>
   </string-array>

   <string-array name="Area">
    <item>Square km</item>
    <item>Square meter</item>
    <item>Square mile</item>
    <item>Hectare</item>
    <item>Acre</item>
    <item>Square yard</item>
    <item>Square foot</item>
    <item>Square inch</item>

   </string-array>

   <string-array name="Area2">
    <item>Square km</item>
    <item>Square meter</item>
    <item>Square mile</item>
    <item>Hectare</item>
    <item>Acre</item>
    <item>Square yard</item>
    <item>Square foot</item>
    <item>Square inch</item>

    </string-array>

布局Xml文件         

  <Spinner
  android:id="@+id/selector1"
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_below="@+id/editText1" />

  <Spinner
  android:id="@+id/spinner_unit"
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="73dp"
  android:entries="@array/units" />

  <Button
  android:id="@+id/equelsbutton"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:layout_centerVertical="true"
  android:layout_toRightOf="@+id/editText1"
  android:background="@color/clear"
  android:text="@string/equels"
  android:textSize="40sp" />

 <Spinner
  android:id="@+id/selector2"
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_alignTop="@+id/selector1" />

 <TextView
  android:id="@+id/textView1"
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:layout_alignBottom="@+id/editText1"
  android:layout_alignLeft="@+id/selector2"
  android:layout_marginLeft="15dp"
  android:textAppearance="?android:attr/textAppearanceMedium" />

Converter_Activity

import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;


public class Converter_Activity extends Activity {

public Spinner spinnerunits;
 public Spinner spinnerpicker1;
 public Spinner spinnerpicker2;

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

spinnerunits = (Spinner) findViewById(R.id.spinner_unit);

spinnerpicker1 = (Spinner) findViewById(R.id.selector1);

spinnerpicker2 = (Spinner) findViewById(R.id.selector2);



}

}

由于

1 个答案:

答案 0 :(得分:0)

您好我修改了Converter_Activity.java类。请尝试使用最新的源代码。它对我来说很好。希望它对你有所帮助。感谢。

<强> Converter_Activity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class Converter_Activity extends Activity {
     public static Spinner spinnerunits;
     public static Spinner spinnerpicker1;
     public static Spinner spinnerpicker2;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.spinnerlayout);
       spinnerunits = (Spinner) findViewById(R.id.spinner_unit);
       spinnerpicker1 = (Spinner) findViewById(R.id.selector1);
       spinnerpicker2 = (Spinner) findViewById(R.id.selector2);

       ArrayAdapter <String> arrayUnits = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.units));
       arrayUnits.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinnerunits .setAdapter(arrayUnits);

       spinnerunits.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
               if(spinnerunits.getSelectedItem().equals("Temperature"))
           {
               ArrayAdapter <String> s1 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.Temperature));
               s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
               spinnerpicker2.setAdapter(s1);
               spinnerpicker1.setAdapter(s1);
           }else  if(spinnerunits.getSelectedItem().equals("Mass"))
           {
               ArrayAdapter <String> s2 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Mass));
               s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
               spinnerpicker2.setAdapter(s2);
               spinnerpicker1.setAdapter(s2);
           }
           else  if(spinnerunits.getSelectedItem().equals("Speed"))
           {
               ArrayAdapter <String> s3 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Speed));
               s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
               spinnerpicker2.setAdapter(s3);
               spinnerpicker1.setAdapter(s3);
           }
           else  if(spinnerunits.getSelectedItem().equals("Area"))
           {
               ArrayAdapter <String> s4 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Area));
               s4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
               spinnerpicker2.setAdapter(s4);
               spinnerpicker1.setAdapter(s4);
           }
           else  if(spinnerunits.getSelectedItem().equals("Time"))
           {
               ArrayAdapter <String> s5 = new ArrayAdapter <String> (getBaseContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Time));
               s5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
               spinnerpicker2.setAdapter(s5);
               spinnerpicker1.setAdapter(s5);
           }
           }

           @Override
            public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
            }  
       });
   }     
}

请尝试使用此代码并告诉我们。感谢。