我一直绞尽脑汁想弄清楚为什么这不会起作用。我需要在微调器中返回所选项目的索引(实际上有4个微调器但是甚至不能使用一个)。
这是XML文件
<?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: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="com.agcinstruments.lab41.MainActivity"
android:background="@color/Navy">
<ImageView
android:id="@+id/resistor2"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:src="@drawable/resistor2"/>
<TextView
android:background="@color/Navy"
android:layout_marginTop="36dp"
android:layout_marginLeft="120dp"
android:layout_width="25dp"
android:layout_height="73dp"
/>
<TextView
android:background="@color/Navy"
android:layout_marginTop="36dp"
android:layout_centerHorizontal="true"
android:layout_width="25dp"
android:layout_height="73dp"
/>
<TextView
android:id="@+id/q"
android:background="@color/Navy"
android:layout_marginTop="36dp"
android:layout_marginLeft="208dp"
android:layout_width="25dp"
android:layout_height="73dp"
/>
<TextView
android:background="@color/Navy"
android:layout_marginTop="26dp"
android:layout_marginLeft="257dp"
android:layout_width="25dp"
android:layout_height="94dp"
/>
<Spinner
android:id="@+id/Spinner1"
android:layout_below="@+id/resistor2"
android:layout_width="85dp"
android:entries="@array/color_array1"
android:layout_height="wrap_content"
android:background="@color/White"/>
<Spinner
android:id="@+id/Spinner2"
android:layout_below="@id/resistor2"
android:layout_toRightOf="@+id/Spinner1"
android:layout_width="85dp"
android:entries="@array/color_array2"
android:layout_height="wrap_content"
android:background="@color/White"/>
<Spinner
android:id="@+id/Spinner3"
android:layout_below="@id/resistor2"
android:layout_toRightOf="@+id/Spinner2"
android:layout_width="85dp"
android:entries="@array/color_array3"
android:layout_height="wrap_content"
android:background="@color/White"/>
<Spinner
android:id="@+id/Spinner4"
android:layout_below="@id/resistor2"
android:layout_toRightOf="@+id/Spinner3"
android:layout_width="85dp"
android:entries="@array/color_array4"
android:layout_height="wrap_content"
android:background="@color/White"/>
<Button
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Spinner4"
android:layout_centerHorizontal="true"
android:text="@string/calculate"
android:onClick="onClickCalculate"/>
<TextView
android:id="@+id/resultText"
android:layout_width="50dp"
android:layout_height="50dp"
android:textColor="@color/Red"
android:layout_below="@+id/calculate"
android:layout_centerHorizontal="true"
android:background="@color/White" />
<EditText
android:id="@+id/tolerance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Red"/>
<EditText
android:id="@+id/ohm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Red"/>
</RelativeLayout>
这是java代码
package com.agcinstruments.lab41;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.server.converter.StringToIntConverter;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public Spinner Spinner1;
public Spinner Spinner2;
public Spinner Spinner3;
public Spinner Spinner4;
int value = 0 ;
int position = 0 ;
TextView resultText;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText = (TextView) findViewById(R.id.resultText);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void onClickCalculate(View view) {
position = Spinner2.getSelectedItemPosition(); // This is the problem.I suspect the adaptor is not working.
value = position * 10;
resultText.setText(Integer.toString(value));
//resultText.setBackgroundColor()
}
public void resistorValue2(View view) {
value = value + (1);
}
public void resistorMultiplier(View view) {
value = value + (1);
}
public void resistorTolerance(View view) {
value = value + (1);
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.agcinstruments.lab41/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.agcinstruments.lab41/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
这是我用来填充数组的strings.xml文件。
<resources>
<string name="app_name">lab4.1</string>
<string name="calculate">CALCULATE</string>
<string-array name="color_array1">
<item>BLACK</item>
<item>BROWN</item>
<item>RED</item>
<item>ORANGE</item>
<item>YELLOW</item>
<item>GREEN</item>
<item>BLUE</item>
<item>PURPLE</item>
<item>GREY</item>
<item>WHITE</item>
</string-array>
<string-array name="color_array2">
<item>BLACK</item>
<item>BROWN</item>
<item>RED</item>
<item>ORANGE</item>
<item>YELLOW</item>
<item>GREEN</item>
<item>BLUE</item>
<item>PURPLE</item>
<item>GREY</item>
<item>WHITE</item>
</string-array>
<string-array name="color_array3">
<item>BLACK</item>
<item>BROWN</item>
<item>RED</item>
<item>ORANGE</item>
<item>YELLOW</item>
<item>GREEN</item>
<item>BLUE</item>
<item>PURPLE</item>
<item>GREY</item>
<item>WHITE</item>
</string-array>
<string-array name="color_array4">
<item>BLACK</item>
<item>BROWN</item>
<item>RED</item>
<item>ORANGE</item>
<item>YELLOW</item>
<item>GREEN</item>
<item>BLUE</item>
<item>PURPLE</item>
<item>GREY</item>
<item>WHITE</item>
</string-array>
我不知道很多JAVA这是我希望获得启发的道路上的绊脚石。任何帮助将不胜感激。我花了好几个小时试图找出解决方案。